22

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"
    });
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 4
    Why didn't you look at the documentation? You should have looked up the meaning of every option you use. `async: false` is the _opposite_ of what you're describing. – Lightness Races in Orbit Feb 08 '12 at 00:02

3 Answers3

17

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)

cgp
  • 41,026
  • 12
  • 101
  • 131
  • 18
    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. –  May 22 '09 at 15:08
  • That's good to know anyway. Can you post a reference for that? – cgp May 22 '09 at 15:25
  • 3
    @Monty Nice job pointing out the PHP issue and session_write_close() - worked for me - thank you! – JoshuaDavid Feb 07 '12 at 09:13
  • 3
    Just found this and solved my own simultaneous ajax problem thanks to the session variables thing. Thanks and ++++ all round! – Codecraft May 02 '12 at 23:57
  • Curious what form(s) this limit takes, Ajax error? PHP error? Silent delay? – Bob Stein Jul 28 '13 at 17:24
  • Silent delay, from what I remember, nothing unusual happens -- if you goto http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp you'll see the 5 ready states. If the request is "queued", I *bet* that it stays in state 0 until there is a free slot. – cgp Aug 14 '13 at 22:01
  • any updates to http specification on this? This question is dated. – JohnMerlino Apr 24 '14 at 00:06
3

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
  • 52,327
  • 25
  • 125
  • 193
0

use axois for ajax call mush better and allows for simultaneous ajax request

user1074242
  • 15
  • 1
  • 4