Jquery:当所有嵌套的ajax调用都完成时,调用then函数
我试图在函数和时间的帮助下编写一些嵌套的ajax调用。但目前,我的then函数没有按预期工作。我想在完成所有Ajax调用(包括嵌套调用)后调用then函数。我想在需要时调用then函数Jquery:当所有嵌套的ajax调用都完成时,调用then函数,jquery,ajax,nested,es6-promise,.when,Jquery,Ajax,Nested,Es6 Promise,.when,我试图在函数和时间的帮助下编写一些嵌套的ajax调用。但目前,我的then函数没有按预期工作。我想在完成所有Ajax调用(包括嵌套调用)后调用then函数。我想在需要时调用then函数 this.getDescendants(resourceId[1],resourceData.limit+resourceData.offset); 也完成了所有主要ajax调用。目前,在嵌套ajax调用完成之前,then正在完成 getUserInfo: function(){ $.
this.getDescendants(resourceId[1],resourceData.limit+resourceData.offset);
也完成了所有主要ajax调用。目前,在嵌套ajax调用完成之前,then正在完成
getUserInfo: function(){
$.ajax({
type: "GET",
contentType: "application/json",
url: UserURL,
context: this,
beforeSend: function (xhr) {
$('#fade-wrapper').fadeIn();
xhr.setRequestHeader('Authorization', "Basic "+this._basicCred);
},
success: function(resourceData) {
if(resourceData.resources != undefined && resourceData.resources.length > 0){
var deferreds = [];
for (var i=0; i < resourceData.resources.length; ++i){
var deferred = this.getDescendants(resourceData.resources[i],0);
deferreds.push(deferred);
}
$.when.apply($, deferreds).then(function() {
var currentContext = this[0];
console.log(currentContext);
console.log("Then called.");
});
} else {
alert("No visible resource has been found.");
}
}.bind(this),
error: function(xhr, status, err) {
console.error( status, err.toString());
$('#fade-wrapper').fadeOut();
alert("Error has occurred, please contact administrator in case the problem persists.");
return null;
}.bind(this)
});
},
getDescendants: function(resId,offset){
return $.ajax({
type: "GET",
contentType: "application/json",
data: {"resId":resId},
url: DesedentURL+"?limit=10&offset="+offset,
context: this,
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic "+this._basicCred);
},
success: function(resourceData, textStatus, xhr) {
if(this._resResults == undefined){
this._resResults = [];
}
this._resResults.push(resourceData);
console.log("resourceData");
console.log(resourceData);
if(resourceData.totalResults > (resourceData.limit+resourceData.offset) ){
var resourceId = resourceData.links[0].href.match("resources/(.*)/descendants");
console.log(resourceId);
this.getDescendants(resourceId[1],resourceData.limit+resourceData.offset);
}
}.bind(this),
error: function(xhr, status, err) {
console.error( status, err.toString());
$('#fade-wrapper').fadeOut();
alert("Error has occurred, please contact administrator in case the problem persists.");
return null;
}.bind(this)
});
}
getUserInfo:function(){
$.ajax({
键入:“获取”,
contentType:“应用程序/json”,
url:UserURL,
背景:这,,
发送前:函数(xhr){
$(“#淡入包装器”).fadeIn();
xhr.setRequestHeader('Authorization','Basic'+this.\u basicCred);
},
成功:函数(resourceData){
if(resourceData.resources!=未定义&&resourceData.resources.length>0){
风险值递延=[];
对于(变量i=0;i(resourceData.limit+resourceData.offset)){
var resourceId=resourceData.links[0].href.match(“resources/(.*));
console.log(resourceId);
this.getsubstands(resourceId[1],resourceData.limit+resourceData.offset);
}
}.绑定(此),
错误:函数(xhr、状态、错误){
console.error(状态,err.toString());
$(“#淡出包装器”).fadeOut();
警报(“发生错误,如果问题仍然存在,请与管理员联系。”);
返回null;
}.绑定(此)
});
}
您可能想了解更多关于。一种常见的模式是创建一个数组,然后将其传递给Promise。all()Hello Zmart,我试图复制相同的。但问题是,在我的ajax中,我必须根据响应处理一些额外的嵌套ajax调用。在额外的ajax调用中,我面临着这个问题,因为我不知道所有额外的嵌套ajax调用何时才能完成。请将它们包装在promise中,并在ajax调用中使用promise resolve()?您可能想了解更多关于。一种常见的模式是创建一个数组,然后将其传递给Promise。all()Hello Zmart,我试图复制相同的。但问题是,在我的ajax中,我必须根据响应处理一些额外的嵌套ajax调用。在额外的ajax调用中,我面临着这个问题,因为我不知道所有额外的嵌套ajax调用何时才能完成。是否将它们包装在promise中,并在ajax调用中使用promise resolve()?