0

这可能是非常基本的,但我似乎无法为我的案例找到明确的答案。

我想知道如何确保在其余工作流程可以继续之前执行并完成一个功能

在我下面的脚本中,警报触发为“1”、“3”、“2”,我希望在脚本进入 up.settings.multipart_params 之前完成getsS3policy(file)

preinit: {

            UploadFile: function (up, file) {
                getS3Policy(file);
                alert("3");
                    up.settings.multipart_params = {
                        key: path,
                        filename: file.name,
                        AWSAccessKeyId: 'AKIAJIRIA6XD4V5MFWCA',
                        acl: 'private',
                        policy: policy,
                        signature: signature,
                        success_action_status: '201'
                    }

            }
        },

    });
    function getS3Policy(file) {
        alert("1");
        data = { 'alc': 'private', 'bucket': 'PhotojimaDev', 'key': path, 'file': file.name };
        $.ajax({
            url: '/desktopmodules/uploader/API/upload/getPolicy',
            type: 'POST',
            data: data,
            success: function (response) {
                console.log(response);
                if (response) {
                    policy = response.policy;
                    signature = response.signature;
                    alert("2");
                }
                else {
                    return "Error";
                }
            }
        });
4

2 回答 2

1

您的 ajax 调用的success功能是回调。如果您将您的回调放入up.settings.multipart_params该回调中,这就是一旦收到数据就会运行的内容,它只会在收到数据后运行。

$.ajax({
        url: '/desktopmodules/uploader/API/upload/getPolicy',
        type: 'POST',
        data: data,
        success: function (response) {
            console.log(response);
            if (response) {
                policy = response.policy;
                signature = response.signature;
                alert("2");
                up.settings.multipart_params = {
                    key: path,
                    filename: file.name,
                    AWSAccessKeyId: 'AKIAJIRIA6XD4V5MFWCA',
                    acl: 'private',
                    policy: policy,
                    signature: signature,
                    success_action_status: '201'
                }
            }
            else {
                return "Error";
            }
        }
    });
于 2013-02-08T22:18:49.860 回答
1

您正在寻找 jQuery ajax 的回调。见http://api.jquery.com/jQuery.ajax/

于 2013-02-08T22:15:55.790 回答