weixin_33736048 2015-08-24 19:03 采纳率: 0%
浏览 34

并发Ajax调用?

I need to call a particular PHP script on my server several times. As each call will take some time (e.g. about .5 seconds) and each call is independent of each other, I want to call the scripts concurrently. Right now, I have something like this:

$(document).ready(function() {

    $.ajax({
      url: 'main.php',
      method: 'POST',
      data: { foo: 'foo' },
      success: function(result) {
        console.log(result);
      }
    });

    $.ajax({
      url: 'main.php',
      method: 'POST',
      data: { bar: 'bar' },
      success: function(result) {
        console.log(result);
      }
    });

});

Instead of making these calls sequentially, I want to do them concurrently. How do I do that?

  • 写回答

3条回答 默认 最新

  • weixin_33674976 2015-08-24 19:06
    关注

    Set your async = true on your AJAX calls to make them asynchronous.

    $.ajax({
        async: "true",
          url: 'main.php',
          method: 'POST',
          data: { foo: 'foo' },
          success: function(result) {
            console.log(result);
          }
        });
    
        $.ajax({
        async: "true",
          url: 'main.php',
          method: 'POST',
          data: { bar: 'bar' },
          success: function(result) {
            console.log(result);
          }
        });
    
    评论

报告相同问题?