【问题标题】:jQuery Promise for Ajax call用于 Ajax 调用的 jQuery Promise
【发布时间】:2014-09-13 06:19:44
【问题描述】:

我认为/希望我遗漏了有关 Promise 编程范式的一些内容。我在 jQuery 上运行以下代码,因为我想从 URL_1then 获取 data (成功时)以获取 data2 来自 URL_2。其他变量来自这段代码的上下文。

但是,我得到的是 URL_1 两次的数据!!

.ajax({
    url: URL_1,
    type: "GET",
    async: false,
    cache: false
}).then(function (data) {
    myObj = process(otherObj, data, URL_1);
    return $.ajax({
        url: URL_2,
        type: "GET",
        async: false,
        cache: false
    });
}).done(function (data2) {
    myObj2 = process_more(data2, URL_2, someObj);
    myCounter--;
    if (myCounter== 0) {
        console.log("%%%% COMPLETE %%%%");
    }
});

提前感谢您的宝贵时间!!

平移

【问题讨论】:

  • 不要使用async: false。它甚至可能导致 promise 出现问题。
  • @Radek - 这就是我的想法,我很确定你不能向 .then 内部的链返回新的承诺,但测试表明你实际上可以 -> jsfiddle.net/4mm4E
  • 那么可能只需删除async: false 部分,它应该可以工作。
  • 你可能是对的,但async: false 仍然可以工作。我检查了 jQuery 版本,它在 1.8 之前,这意味着 promise/deferred 模型在原始 promise 模型中被“破坏”了。我用最新版本替换了 jquery,现在它工作正常。

标签: javascript jquery ajax promise jquery-1.7


【解决方案1】:

事实证明,只要 jQuery 版本大于 1.8(我知道但没有注意到我使用的是最新版本),代码就可以正常工作。我用最新版本替换了 jQuery,一切都按预期工作。然而,@Bergi 关于async:false 无用甚至导致问题是正确的。

在早期版本的 jQuery 中,promise/deferred 模型是“损坏的”并且不能按预期工作/应该 w.r.t.原始承诺模型 (https://www.promisejs.org/)。

另见:http://api.jquery.com/deferred.then/

【讨论】:

【解决方案2】:

我是这样解决的:

for (var i = 0; i < json.length; i++) {

    if(json[i].university_name !== '' && json[i].state_code !== ''){

        $.when(
            $.ajax({
                async: false,
                url: "validateUniversityExist.php",
                method: 'post',
                dataType: 'json',
                data:{
                    'name': json[i].university_name,
                    'state_code' : json[i].state_code
                }
            })).then(function( resp, textStatus, jqXHR ) {

                if(resp.id_university !== '' || resp.id_university !== undefined){

                    if (json[i].record_status == 3){

                        $.ajax({
                            url: "createNewBenefits.php",
                            method: 'post',
                            dataType: 'json',
                            data:{
                                'id_university': resp.id_university,
                                'state_code' : json[i].state_code,
                                'updated' : json[i].updated,
                                'id_visa' : json[i].visa_type,
                                'record_status' : json[i].record_status,
                                'mandatory' : json[i].mandatory,
                                'aca' : json[i].aca
                            }
                        }); // end insert complete record ajax

                    }

                }

            }); // end university when ajax
    }


} // end for

使用when 和then。 https://api.jquery.com/jquery.when/

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2016-03-11
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多