我正在发送多个ajax请求,并且如果所有请求都成功,则希望获得回调。我发现了,$.when($.ajax(), [...]).then(function(results){},[...]);
但是只有当您事先知道要做什么时,它才起作用。就我而言,它取决于用户输入。
我尝试了以下方法,但不确定在哪里或如何$.when
使用:
$.when(
$('#piecesTable tr').not(':first').each(function(){
// ...some prep...
$.ajax({
// ...args here...
});
})
).then(function() {
// All requests are done
});
如何将所有这些单独$.ajax
调用的结果用于$.when
?还是我以其他方式处理?
我认为您要查找的内容的总体结构如下所示:
var requests = [];
// Populate requests array with ajax requests.
requests.push($.ajax({
// ...
}));
// Add as many requests as you want to the array.
$.when.apply($, requests).done(function() {
var args = $.prototype.slice.call(arguments);
// args should contain the results of your ajax requests.
// Do whatever with the results.
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句