如何检查多个ajax请求的完成情况
How to check completion of multiple ajax requests?
我正在执行多个(3)ajax请求。我如何检查它们是否都已成功返回,然后才对输出进行处理。我想过把请求链接起来,但是这样请求就不会同时执行了。
最终我试图加载三个表,并在加载后,动画他们的位置。动画应该只有在所有的表格都被加载之后才会发生。
另外,处理多个ajax调用的良好实践是什么?此时,当它只是简单地重复前一个句子时,语法看起来并不那么"漂亮"。
谢谢!
下面是我的代码:
// THIS TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true',
success: function(output) {
$('#bookingTableThis').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTableThis').html(output);
});
}
});
// PREV TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true&dateShift=prev',
success: function(output) {
$('#bookingTablePrev').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTablePrev').html(output);
});
}
});
// NEXT TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true&dateShift=next',
success: function(output) {
$('#bookingTableNext').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTableNext').html(output);
});
}
});
创世纪的答案是正确的,但不幸的是它产生了另一个问题。我在Javascript中多次遇到传递变量的问题——在这种情况下也是如此。每个ajax调用的输出不喜欢显示在"then"函数中:
var outputThis;
var outputPrev;
var outputNext;
$.when(function(){
// THIS TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true',
success: function(output) { outputThis = output; }
});
// PREV TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true&dateShift=prev',
success: function(output) { outputPrev = output; }
});
// NEXT TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true&dateShift=next',
success: function(output) { outputNext = output; }
});
}).then(function(){
// THIS
$('#bookingTableThis').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTableThis').html(outputThis);
});
// PREV
$('#bookingTablePrev').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTablePrev').html(outputPrev);
});
// NEXT
$('#bookingTableNext').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTableNext').html(outputNext);
});
});
$.when(function(){// THIS TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true',
success: function(output) {
$('#bookingTableThis').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTableThis').html(output);
});
}
});
// PREV TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true&dateShift=prev',
success: function(output) {
$('#bookingTablePrev').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTablePrev').html(output);
});
}
});
// NEXT TABLE
$.ajax({
type: 'POST',
url: 'includes/content/bookingsystem/b_dayview.php',
data: $(this).attr('name') + '=true&dateShift=next',
success: function(output) {
$('#bookingTableNext').animate({
left: direction + '=820'
}, 500, function() {
$('#bookingTableNext').html(output);
});
}
});
}).then(function(){
alert('all of them were done');
});
为了2012年来这里的人的利益,我发现上面答案的语法已经更改为如下内容
$.when(
$.ajax({
type: 'GET',
url: '/api/data/jobtypes',
dataType: "json",
success: loadJobTypes
}),
$.ajax({
type: 'GET',
url: '/api/data/servicetypes',
dataType: "json",
success: loadServiceTypes
}),
$.ajax({
type: 'GET',
url: '/api/data/approvaltypes',
dataType: "json",
success: loadApprovalTypes
}),
$.ajax({
type: 'GET',
url: '/api/data/customertypes',
dataType: "json",
success: loadCustomerTypes
})
).done(function(){
alert("all done");
});
预初始化一个包含3个元素的全局数组,然后将结果存储在该数组中。当数组被完全填充后,更新页面。
相关文章:
- ajax请求的顺序总是不同的
- 从ajax请求中获取javascript对象
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- JavaScript代码未正确检查ajax请求
- node.js请求数据事件未在CORS ajax调用中触发
- jQuery Ajax GET请求工作不正常
- "日期“;AJAX请求返回的类型值未定义
- Django - 响应请求 AJAX
- 使用 jQuery 的 CORS 请求 - $.ajax()
- 如果无线电值为 ==1,则请求 ajax
- 如何处理对循环中发出的多个异步请求(AJAX 调用)的响应
- POST请求ajax jquery错误
- CasperJS don'我没有请求AJAX
- 使用这种Facebook风格的Lightbox-请求AJAX-在一个页面上覆盖多个按钮
- 400错误请求ajax post请求
- 如何在开始发送请求ajax之前延迟3秒
- 与jQuery同时请求AJAX
- 使用POST请求AJAX发送json对象