我有几个ajax方法,我想在成功完成所有这些ajax调用后执行一些代码。我无法改变或重新定义ajax方法。 请让我知道,如何实现这一目标。
我尝试使用WHEN,但它会立即调用,而不是等待所有调用完成。(正如所建议的,一旦我在loadData1()中添加了返回,它就可以正常工作。)
现在我的问题是,如果任何请求(loadData1()或loadData2())有错误,那么“then()”没有被执行。请告诉我,如何实现这一目标。
var load1 = loadData1();
var load2 = loadData2();
var load3 = loadData3();
var load4 = loadData4();
$.when(load1, load2, load3,load4).then(function () {
console.log("All done");
});
function loadData1() {
return $.getJSON("http://10.1.2.3/cgi-bin/GetData1.cgi", function (data) {
console.log(data);
});
}
由于
答案 0 :(得分:2)
您可以使用
之类的功能 function first() {
return $.ajax(...);
}
function second(data, textStatus, jqXHR) {
return $.ajax(...);
}
function third(data, textStatus, jqXHR) {
return $.ajax(...);
}
function main() {
first().then(second).then(third);
}
查看此详细信息jquery-promises
答案 1 :(得分:1)
试试这个
var promise = loadData1();
var load2 = loadData2();
var load3 = loadData3();
var load4 = loadData4();
promise.then(loadData2).then(loadData3).then(loadData4).then(function (E) {
//
});
function loadData1() {
$.getJSON("http://10.1.2.3/cgi-bin/GetData1.cgi", function (data) {
console.log(data);
});
var deferred = new $.Deferred();
deferred.resolve();
return deferred.promise();
}
答案 2 :(得分:1)
您应该在功能中返回一些内容。这是第一件事。第二个等待时,您必须等待功能结果,而不是-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
使用load1, load2, load3, load4
。
下面是一个模拟4个延迟函数(你的ajax请求)的例子。
load1(), load2(), load3(), load4()

// load1, load2, load3, load4 are defined to simulate promises and some work behind it
// here is var load1 = loadData1();
var load1 = function () {
return $.Deferred(function(defer) {
setTimeout(function() {
$('#log').append('<div>loadData1()</div>');
defer.resolve();
}, 150);
});
}
// here is var load2 = loadData2();
var load2 = function () {
return $.Deferred(function(defer) {
setTimeout(function() {
$('#log').append('<div>loadData2()</div>');
defer.resolve();
}, 600);
});
}
// here is var load3 = loadData3();
var load3 = function () {
return $.Deferred(function(defer) {
setTimeout(function() {
$('#log').append('<div>loadData3()</div>');
defer.resolve();
}, 300);
});
}
// here is var load4 = loadData4();
var load4 = function () {
return $.Deferred(function(defer) {
setTimeout(function() {
$('#log').append('<div>loadData4()</div>');
defer.resolve();
}, 200);
});
}
$(document).ready(function() {
$.when(load1(), load2(), load3(), load4())
.then(function () {
$('#log').append('<div>All done.</div>');
});
});
&#13;
答案 3 :(得分:1)
您可以尝试使用像
这样的实用程序功能Dim conServer As ADODB.Connection
Dim rstResult As ADODB.Recordset
Dim strSQL As String
Set conServer = New ADODB.Connection
conServer.ConnectionString = "PROVIDER=SQLOLEDB; " _
& "DATA SOURCE=192.168.0.204; " _
& "INITIAL CATALOG=REPORTDB2; " _
& "User ID=sa;" _
& "Password="
conServer.Open
Set rstResult = New ADODB.Recordset
strSQL = "set nocount on; "
strSQL = strSQL & "select * from Table1;"
rstResult.ActiveConnection = conServer
rstResult.Open strSQL
MsgBox rstResult.RecordCount, , rstResult.Fields(1).Value
With rstResult
.MoveFirst
Do Until rstResult.EOF
Debug.Print abc & "," & .Fields(0) & "," & .Fields(1).Value
.MoveNext
Loop
End With
然后
function allCompleted(array) {
var deferred = jQuery.Deferred(),
counter = array.length,
results = [];
$.each(array, function(i, item) {
item.always(function() {
results[i] = [].slice.call(arguments, 0)
if (--counter == 0) {
deferred.resolveWith(undefined, results);
}
});
});
return deferred.promise();
}
演示:Fiddle