我需要进行多次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 请求完成。任何帮助,将不胜感激。
答案如下: 复制自jQuery.当使用可变数量的参数进行故障排除时 https://stackoverflow.com/questions/9865586/jquery-when-troubleshooting-with-variable-number-of-arguments
// 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
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)