【发布时间】:2013-07-10 18:18:48
【问题描述】:
我似乎无法处理 jQuery 的 $.Deferred 处理 AJAX 调用。
我要做的是执行三个 AJAX 调用,每个调用都对返回的数据进行一些处理。第三次AJAX调用的成功调用需要前两次调用的处理完成,但前两次调用的顺序无关紧要。
这是我的代码,a jsFiddle:
var firstAjax = $.getJSON('/echo/json/')
.done(
function(data, textStatus, jqXHR){
//do some initialization here based on the data
alert(1);
return jqXHR.promise();
}
);
var secondAjax = $.getJSON('/echo/json/')
.done(
function(data, textStatus, jqXHR){
//do some initialization here based on the data
alert(2);
return jqXHR.promise();
}
);
$.when(firstAjax, secondAjax)
.done(
$.getJSON('/echo/json/')
.done(
function(data, textStatus, jqXHR){
//do some initialization here that relies on the initialization of the first and second calls being complete
alert(3);
}
)
);
有时,但并非总是,“3”会在“1”和“2”之前发出警报。立即执行第三个 AJAX 调用没有问题,但它的 done 处理程序需要最后执行。
【问题讨论】:
标签: javascript jquery jquery-deferred