最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why aren't my JQuery .ajax requests being made in parallel? - Stack Overflow

programmeradmin0浏览0评论

I am trying to make two ajax requests in parallel using jQuery like this:

    var sources = ["source1", "source2"];

    $(sources).each(function() {
      var source = this;
      $.ajax({
        async: true,
        type: "POST",
        data: {post: "data", in: "here"},
        url: "/my/url/" + source,
        success: function(data) {
          process_result(data);
        }
      });
    });

I got the basic structure from this question, but my requests still aren't being made in parallel. "source1" takes a while to complete, and I can see on the server that the second request isn't made until the first is completed.

As far as I can tell, I don't have any other active requests, so I don't think it's a problem with the maximum number of parallel requests for the browser. Am I missing something else here?

I am trying to make two ajax requests in parallel using jQuery like this:

    var sources = ["source1", "source2"];

    $(sources).each(function() {
      var source = this;
      $.ajax({
        async: true,
        type: "POST",
        data: {post: "data", in: "here"},
        url: "/my/url/" + source,
        success: function(data) {
          process_result(data);
        }
      });
    });

I got the basic structure from this question, but my requests still aren't being made in parallel. "source1" takes a while to complete, and I can see on the server that the second request isn't made until the first is completed.

As far as I can tell, I don't have any other active requests, so I don't think it's a problem with the maximum number of parallel requests for the browser. Am I missing something else here?

Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked Mar 29, 2010 at 18:38 Ryan OlsonRyan Olson 2,8264 gold badges31 silver badges36 bronze badges 3
  • What do you see in Fiddler or Firebug? – SLaks Commented Mar 29, 2010 at 18:40
  • I just re-tried my answer from that linked question and added a sleep(4); in the PHP page - still worked as expected. All 4 requests fired at once and all took ~4 seconds to complete. – Peter Bailey Commented Mar 29, 2010 at 18:45
  • Backend was Catalyst running in my development sandbox without the --fork option. The Net panel in Firebug didn't display the second request until the first had completed. – Ryan Olson Commented Mar 30, 2010 at 12:55
Add a comment  | 

3 Answers 3

Reset to default 13

jQuery does not queue AJAX requests. If you're sure you're not making any other requests your end, how about the server? Maybe it only has one worker?

EDIT: And just to make sure, I tested it with a script that launches 2 AJAX POST requests to a PHP script which sleeps for 5 seconds. They were not queued.

are you using php? are you using session_start()? sessions cannot be opened in parallel by multiple requests, to they will wait one after another to finish what they're doing.

What if you used $.post instead of $.ajax. This works for me.

var sources = ["source1", "source2"];

$(sources).each(function() {
  var source = this;
  $.post("/my/url/" + source, {post:"data", in:"here"}, function(data) {
      process_result(data);
  });
)};
发布评论

评论列表(0)

  1. 暂无评论