如何检查多个ajax请求的完成情况

How to check completion of multiple ajax requests?

本文关键字:请求 ajax 情况 何检查 检查      更新时间:2023-09-26

我正在执行多个(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个元素的全局数组,然后将结果存储在该数组中。当数组被完全填充后,更新页面。