[英]JQuery/Ajax: How to combine several ajaxes into one async ajax with response?
異步使用$。
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');
}
每個ajax1,ajax2或ajax3都可以同步或異步
如果不正常,那么我希望在控制台輸出中看到下一個順序 :
開始
結束
成功(或錯誤)
但是實際輸出始終是:
開始
成功(或錯誤)
結束
您永遠不要使用同步ajax。 –阿德尼奧
問題是我的某些ajax是同步的,因為它們的結果是依賴的。
ajax1完成后無法調用ajax2。 例如$ ajax(){.....,完成:function(){ajax2調用} – user3509208
因此,我使它們異步,並為那些依賴數據的人完成了內部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: ...)
);
}
現在它正在工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.