[英]JQuery/Ajax: How to combine several ajaxes into one async ajax with response?
Is it possible to use $.when asynchronously: 异步使用$。
init: function() {
console.log('begin');
$.when(
{async: true},
this.ajax1(),
this.ajax2(),
this.ajax3()
).done(function() {
console.log('success');
}, function () {
console.log('error');
});
console.log('end');
}
each ajax1, ajax2 or ajax3 can be either sync or async 每个ajax1,ajax2或ajax3都可以同步或异步
For not it's not working then I expect, I want to see the next order in console output: 如果不正常,那么我希望在控制台输出中看到下一个顺序 :
begin 开始
end 结束
success (or error) 成功(或错误)
But actual output is always: 但是实际输出始终是:
begin 开始
success (or error) 成功(或错误)
end 结束
you should never use synchronous ajax.
您永远不要使用同步ajax。 – adeneo
–阿德尼奥
The problem was that some of my ajaxes were synchronous because their results are depended. 问题是我的某些ajax是同步的,因为它们的结果是依赖的。
can't you call ajax2 when ajax1 is completed.
ajax1完成后无法调用ajax2。 for eg $ajax() { ....., complete: function() { ajax2 call} – user3509208
例如$ ajax(){.....,完成:function(){ajax2调用} – user3509208
So I made them asynchronous and made inner ajaxes on completed for those who data are depended: 因此,我使它们异步,并为那些依赖数据的人完成了内部ajax的操作:
init: function() {
console.log('begin');
$.when(
{async: true},
this.ajax1(),
this.ajax2()
).done(function() {
console.log('success');
}, function () {
console.log('error');
});
console.log('end');
}
ajax1: function() {
return $.ajax('url', {
async: true,
success: ... ,
error: ...)
);
}
ajax2: function() {
return $.ajax('url', {
async: true,
success: ajax3(),
error: ...)
);
}
ajax3: function() {
return $.ajax('url', {
async: true,
success: ... ,
error: ...)
);
}
And now it's working! 现在它正在工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.