Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

jQuery: Making simultaneous ajax requests, is it possible?

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"
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...