【发布时间】:2016-04-18 22:18:42
【问题描述】:
您好,我有多个 ajax 调用,我想做的是异步触发所有它们,然后等到它们都完成后再处理返回的数据。我尝试使用 $.when 但无济于事。这是我的代码:
//form_identifier_list is my flag to get the multiple forms in my html page
function test(form_identifier_list){
var deffereds = [];
$.each(form_identifier_list, function(key,value){
var this_form = $(this).parents('.ajaxed_form');
deffereds.push( $.post($(this_form).attr("action"), $(this_form).serializeForm()) );
});
$.when.apply($, deffereds).done(function(){
//how to output response obj?? i tried console.log(data) to no avail
}).fail(function(){
}).always(function(){
});
}
我还注意到我的 ajax 请求没有响应(我在浏览器上进行了验证)。
有没有办法让多个 ajax 调用异步触发,然后等到所有调用完成后再访问数据?
谢谢
【问题讨论】:
-
你的代码看起来没问题,除了你在承诺链上使用了
.always()。您需要描述代码是如何失败的,和/或产生的任何错误。 -
I also noticed my ajax requests do not have reponse- 如果您的 ajax 请求没有响应 - 那么您希望登录您的 done 函数? -
当我多次调用我的 $.post 时,最后一个会有响应。我会把我的代码供你参考
标签: javascript jquery ajax asynchronous