tags:

views:

1233

answers:

4

I am using jQuery to try and retrieve multiple pieces of data at the same time. The background of the requirement is that different bits of data take various lengths of time to become available, so I want to display each piece as it is returned.

The problem is that the requests seem to 'queue', the next request does not go until the previous one has returned. After extensive reading, it seems the option 'async: false' might be what I'm after, but this seems to make no difference.

From the TCP/IP debug I can see the browser does not initiate more than one connection, it uses the same connection when the prior request has returned.

I've seen many sites in my time which load data over ajax simultaneously, so obviously it's possible, but I'm tearing my hair out trying to get this working.

Here is my code:

$.ajax({
 type: "GET",
 async: false,
 url: "foo1.php"
 });
$.ajax({
 type: "GET",
 async: false,
 url: "foo2.php"
 });
$.ajax({
 type: "GET",
 async: false,
 url: "foo3.php"
 });
+5  A: 

You want asynchronous (which is the default). The browser limits you to two requests at any given time. This is part of the HTTP specification. (HTTP 1.1 specification, section 8.1.4)

Putting the requests into the queue is your best option.

I should note that this can be overridden in Firefox and probably some other browsers. (but as it's not standard, it doesn't help you much)

altCognito
Thanks. You made me stop looking where there never was a problem. Turns out this is related to PHP and the way it sequentially processes requests which use the same session variables due to locking of the session (why the hell it doesn't lock on write, instead of during the whole execution seems madness). Anyway, a very simple 'session_write_close()' has fixed everything.Cheers.
That's good to know anyway. Can you post a reference for that?
altCognito
A: 

Browsers limit you to two simultaneous requests. You might want to use a queue manager, like this: http://www.protofunc.com/scripts/jquery/ajaxManager/

Jon Galloway
The browser behaviour is fine, it was PHP at 'fault'.
A: 

With a same scenario as above. I'm using Spring MVC with JSON return type, all request are async request. When page is loaded i need to make two simultaneous ajax request to two different URL's. Each request response is not dependant on each others. In firebug debugger console i can see that only one request is sent to server and other is not triggered at all. In this case do i still need to use a plugin to queue the response or is there any other way to fix this problem.

A: 

I'm facing the same problem, but using Asp.Net MVC. Particularly I'm trying to implement a poor man server push with a xhr request pending on a AsyncAction (from MVC futures) and other sync calls delivered to regular action methods. The issue is that the browser doesn't send other requests at all unless the pending one returns.

siroman