完成后jQuery执行函数

时间:2013-02-08 22:14:06

标签: jquery

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

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

在我的脚本中,警报点击为“1”,“3”,“2”,我希望在脚本进入up.settings.multipart_params

之前,我想让getsS3policy(文件)完成
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";
                }
            }
        });

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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";
            }
        }
    });