简体   繁体   English

双ajax请求响应

[英]Double ajax request response

I'm using ajax successives requests and I need do a callback when all the successives requests are done 我正在使用ajax连续请求,当所有连续请求完成后,我需要做一个回调

function doAjaxRequest(data, id) {
    // Get payment Token
    return $.ajax({
        type: "POST",
        url: 'exemple1.php',
        data: data
        success: function(msg){
            $.ajax({
                type: "POST",
                url: 'exemple2.php',
                data: msg,
                success: function(msgr) {
                    document.getElementById(id).value=msgr;
                },
                error:function (xhr, status, error) {
                    //Do something
                }
            });
        },
        error:function (xhr, status, error) {
            //Do something
        }
    });
}

$.when(
    doAjaxRequest(data, "input-1"),
    doAjaxRequest(otherData, "input-2")
).done(function(a1, a2){
    //Need do something when both second ajax requests (example2.php) are finished
}

With this code, the done function is call before my calls to "exemple2.php" are succeeded. 使用此代码,将在成功调用“ exemple2.php”之前调用done函数。

How can I wait for that? 我要如何等待?

Thanks for answering! 谢谢回答!

function doAjaxRequest(data, id) {
    // Get payment Token
    return new Promise(function(resolve,reject){
        $.ajax({
        type: "POST",
        url: 'exemple1.php',
        data: data
        success: function(msg){
            $.ajax({
                type: "POST",
                url: 'exemple2.php',
                data: msg,
                success: function(msgr) {
                    document.getElementById(id).value=msgr;
                    resolve();
                },
                error:function (xhr, status, error) {
                    //Do something
                    reject();
                }
            });
        },
        error:function (xhr, status, error) {
            //Do something
            reject();
        }
    });
    });
}



Promise.all([
    doAjaxRequest(data, "input-1"),
    doAjaxRequest(otherData, "input-2")])
.then(function(values){
    //Need do something when both second ajax requests (example2.php) are finished
}

Your sub ajax request is independant of the first ajax result, then the call to example2 is completely separated from the $.when() promise.abort Just try to use the fact that jquery $.ajax return promise like object Here my code from plnkr 您的子ajax请求与第一个ajax结果无关,然后对example2的调用与$ .when()promise.abort完全分开。只需尝试使用jquery $ .ajax返回promise这样的事实,这是我来自plnkr的代码

// Code goes here
function doAjaxRequest(data, id) {
    // Get payment Token
    return $.ajax({
        type: "GET",
        url: 'example1.json',
        data: data
    }).then(function(msg, status, jqXhr) {
      return $.ajax({
          type: "GET",
          url: 'example2.json',
          data: msg
      });
    }).done(function(msgr) {
        console.log(msgr);
        return msgr;
    });
}

var data = {foo:'bar'};
var otherData = {foo2:'bar2'};

$.when(
    doAjaxRequest(data, "input-1"),
    doAjaxRequest(otherData, "input-2")
).done(function(a1, a2) {
    console.log(a1, a2);
    //Need do something when both second ajax requests (example2.php) are finished
});

Attention, I replace POST by GET and use exampleX.json files for my tests on plnkr 注意,我将POST替换为GET并在exampleX.json文件中对exampleX.json进行测试

You can test it here : https://plnkr.co/edit/5TcPMUhWJqFkxbZNCboz 您可以在这里进行测试: https : //plnkr.co/edit/5TcPMUhWJqFkxbZNCboz

Return a custom deferred object, eg: 返回一个自定义的延迟对象,例如:

function doAjaxRequest(data, id) {
    var d = new $.Deferred();
    // Get payment Token
    $.ajax({
        type: "POST",
        url: 'exemple1.php',
        data: data
        success: function(msg){
            $.ajax({
                type: "POST",
                url: 'exemple2.php',
                data: msg,
                success: function(msgr) {
                    document.getElementById(id).value=msgr;
                    d.resolveWith(null, [msgr]); // or maybe d.resolveWith(null, [msg]);
                },
                error:function (xhr, status, error) {
                    //Do something
                    d.reject();
                }
            });
        },
        error:function (xhr, status, error) {
            //Do something
            d.reject();
        }
    });
    return d;
}

Now, i'm not sure what is your expected datas passed to $.when().done() callback. 现在,我不确定传递给$.when().done()回调的预期数据是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM