Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery进行动态ajax调用并等待它们完成_Jquery - Fatal编程技术网

jQuery进行动态ajax调用并等待它们完成

jQuery进行动态ajax调用并等待它们完成,jquery,Jquery,我需要进行一些ajax调用(确切的数量是可变的),然后等待它们全部完成。我目前的代码如下: ajaxRequests = new Array(); ajaxRequests.push(function(){ return jQuery.post(url: "someUrl", dataType: "json", data

我需要进行一些ajax调用(确切的数量是可变的),然后等待它们全部完成。我目前的代码如下:

ajaxRequests = new Array();

ajaxRequests.push(function(){
                return jQuery.post(url: "someUrl",
                                    dataType: "json",
                                    data:  yourJsonData
            });



jQuery.when.apply(jQuery, ajaxRequests).done(function(){
    alert("ajax requests done");
});

不幸的是,上面的代码没有等待ajax请求完成。任何帮助都将被告知。

答案如下:抄袭自


检查以下答案是否有助于确保在JSONDATA后面加上括号。这可能是导致问题的原因。当您至少有两个请求要发送时,此功能运行良好。否则,在一个或多个查询中会出现不一致的行为:(
// Array of requests
var requests = Array();
requests.push($.get('responsePage.php?data=foo'));
requests.push($.get('responsePage.php?data=bar'));

var defer = $.when.apply($, requests);
defer.done(function(){

    // This is executed only after every ajax request has been completed

    $.each(arguments, function(index, responseData){
        // "responseData" will contain an array of response information for each specific request
    });

});