views:

302

answers:

3

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?

+9  A: 

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?

Just to make sure, I ran this test: http://sb.mattlunn.me.uk/concurrent.html. This launches 4 AJAX POST requests. The PHP script which is the target sleeps for 5 seconds. You can see that they are not queued.

Matt
Seems like a server related issue.
BYK
Yes, was running my web server in my development sandbox without the --fork option, so it was only handling one request at a time.
Ryan Olson
A: 

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);
  });
)};
Jonathan Mayhak
A: 

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.

Quamis