我有三个ajax调用,它为我提供数据,我将其推入数组。
第二个在第一个之后调用(在它的成功函数中),这样数组就不会被填充为异步。
我的目标是使用jQuery done()函数(或类似函数)在最后(在我的代码中标记为“END !!”)进行某种回调。然而,jQuery done()函数在第一级已经达到它的成功函数之后开始了。
var ajaxQuery = $.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {
clients:1
},
success: function(data) {
console.log("1");
for (i = 0; i < data.length; i++) {
var name = data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({name: name, usrid: data[i].usr});
}
$.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {
clients:2
},
success: function(data) {
console.log("2");
for (i = 0; i < data.length; i++) {
var name = "(CRM Lead) - " + data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({name: name, usrid: data[i].usr});
}
$.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {
clients:3
},
success: function(data) {
console.log("3");
for(i = 0; i < data.length; i++) {
var name = "(MXVV Kunden) - " + data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({name: name, usrid: data[i].usr});
}
//END!!!!!!!!
console.log(kontaktdata);
}
});
}
});
}
});
ajaxQuery.done(function(response) {alert("done");});
答案 0 :(得分:1)
您应该使用then
哪些链异步操作,而不是使用done
或success
回调,并为最后一个结果提供承诺。在你的情况下,它看起来像这样:
var kontaktdata = [];
var ajaxQuery = $.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {clients: 1}
}).then(function(data) {
console.log("1");
for (var i = 0; i < data.length; i++) {
var name = data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({name: name, usrid: data[i].usr});
}
return $.ormon('ajax', {
// ^^^^^^ return another promise from the callback
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {clients: 2}
});
}).then(function(data) {
console.log("2");
for (var i = 0; i < data.length; i++) {
var name = "(CRM Lead) - " + data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({name: name, usrid: data[i].usr});
}
return $.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {clients: 3}
});
}).then(function(data) {
console.log("3");
for(var i = 0; i < data.length; i++) {
var name = "(MXVV Kunden) - " + data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({name: name, usrid: data[i].usr});
}
return kontaktdata; //END!!!!!!!!
});
ajaxQuery.done(function(result) {
console.log(result);
alert("done");
});
好的,这很好用,但看起来仍然很乏味。如果只有3个请求要做怎么办?您想在循环中对此进行编码,现在很容易实现:
var ajaxQuery = $.when([]);
for (var client=1; client<=3; client++) (function(client) {
ajaxQuery = ajaxQuery.then(function(kontaktdata) {
return $.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {clients: client}
}).then(function(data) {
console.log(client);
for (var i = 0; i < data.length; i++) {
kontaktdata.push({
name: data[i].adr1 + " " + data[i].adr2,
usrid: data[i].usr
});
return kontaktdata;
});
});
}(client)); // necessary extra scope for closure
ajaxQuery.done(function(result) {
console.log(result);
alert("done");
});
使用promises,它会变得更好 - 你可以平行地发送所有三个请求,然后wait until they're all done,并结果:
var requests = [];
for (var client=1; client<=3; client++)
requests.push($.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {clients: client}
}).then(function(data) {
return data.map(function(d) {
return {name: d.adr1+" "+d.adr2, usrid: d.usr};
});
}));
var ajaxQuery = $.when.apply($, requests).then(function() {
return [].concat.apply([], arguments);
});
ajaxQuery.then(function(result) {
console.log(result);
alert("done");
}, function(err) {
console.error(err + " went wrong");
});
使用promises还有一个额外的好处,就是任何ajax都会失败,或者会捕获异步异常,你最后可以放置一个类似catch
的处理程序。
答案 1 :(得分:0)
您可以使用的是延期承诺(http://api.jquery.com/deferred.promise):
这样的事情应该有效(我没有测试过它!):
var ajaxQuery = function () {
var dfd = new jQuery.Deferred();
$.ormon('ajax', {
[...]
$.ormon('ajax', {
[...]
$.ormon('ajax', {
[...]
success: function(data) {
dfd.resolve(data);
}
});
});
});
return dfd;
};
ajaxQuery().done(function (data) { [...] });
答案 2 :(得分:0)
您可以在嵌套调用上调用done()
方法:
var ajaxQuery = $.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {
clients: 1
},
success: function (data) {
console.log("1");
for (i = 0; i < data.length; i++) {
var name = data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({
name: name,
usrid: data[i].usr
});
}
$.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {
clients: 2
},
success: function (data) {
console.log("2");
for (i = 0; i < data.length; i++) {
var name = "(CRM Lead) - " + data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({
name: name,
usrid: data[i].usr
});
}
$.ormon('ajax', {
url: '../getjson',
trtyp: 'adm1034',
dataType: 'json',
data: {
clients: 3
},
success: function (data) {
console.log("3");
for (i = 0; i < data.length; i++) {
var name = "(MXVV Kunden) - " + data[i].adr1 + " " + data[i].adr2;
kontaktdata.push({
name: name,
usrid: data[i].usr
});
}
//END!!!!!!!!
console.log(kontaktdata);
}
}).done(function (response) {
alert("done");
});
}
});
}
});