使用jQuery Deferred链接AJAX处理程序
Chaining AJAX handlers with jQuery Deferred
我似乎无法处理jQuery对AJAX调用的$.Deferred
处理。
我想做的是执行三个AJAX调用,每个调用都对返回的数据执行一些处理。第三个AJAX调用的成功调用要求前两个调用的处理完成,但前两个呼叫的顺序无关紧要。
这是我的代码,还有一个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);
}
)
);
有时,但并非总是在"1"answers"2"之前提醒"3"。我对立即执行第三个AJAX调用没有问题,但它的done处理程序需要最后执行。
您可以进行
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(function(){
$.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);
}
)
});
您错过了$.when(firstAjax,secondAjax).done(function())这行的"function(){"http://jsfiddle.net/ACBJs/1/
相关文章:
- keyup事件处理程序更改焦点不适用于快速键入
- 提示使用服务器端事件处理程序激活JavaScript
- 将事件处理程序绑定到任何可能的事件
- 正在将事件处理程序添加到不存在的类
- 在AJAX回调和呈现PIXIJS之间处理程序流时遇到了问题
- Adobe Edge:动画完成时添加onComplete处理程序
- 是否可以从输入处理程序中确定输入的类型
- 在循环中附加事件处理程序时出现浏览器性能问题
- 在同一个javascript事件处理程序中调用不同的函数
- 有没有一种方法可以让内联事件处理程序在元素创建后立即执行
- 检查事件处理程序参数
- 实现延迟的jquery更改事件处理程序
- 平均值.on(事件[,选择器][,数据],处理程序)
- getSript在同一对象上多次添加处理程序
- 如何使用Node.js在JavaScript模块文件之间使用事件处理程序
- 如何让React JS点击处理程序在执行时更新DOM
- 如何使jQuery的“bind”或“on”事件处理程序幂等
- Backbone listenTo不将jquery函数作为处理程序进行激发
- 带有参数的Javascript事件处理程序
- Jquery事件处理程序仅适用于匿名函数