jQuery的:使同步Ajax请求,这可能吗? [英] jQuery: Making simultaneous ajax requests, is it possible?

查看:142
本文介绍了jQuery的:使同步Ajax请求,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery,试图在同一时间检索多个数据。的要求的背景是数据的不同位花费的时间变得可用不同的长度,所以我想,以显示每一块,因为它是返回。

的问题是,在请求似乎'队列';接下来的请求不会去,直到一次又恢复了previous。经过广泛的阅读,似乎选项异步:假可能是我后,但这似乎没有什么区别

从TCP / IP调试,我可以看到浏览器不发起多个连接;它使用的时候事先请求返回相同的连接。

我见过许多网站在我的时间,这在AJAX加载数据同步,所以很明显这是可能的,但我撕裂我的头发试图让这个工作。

下面是我的code:

  $。阿贾克斯({
    键入:GET,
    异步:假的,
    网址:foo1.php
    });
$阿贾克斯({
    键入:GET,
    异步:假的,
    网址:foo2.php
    });
$阿贾克斯({
    键入:GET,
    异步:假的,
    网址:foo3.php
    });
 

解决方案

您想异步的(这是默认值)。浏览器限制你在任何时候两个请求。这是HTTP规范的一部分。 ( HTTP 1.1规范,第8.1.4节

把请求放入队列中是最好的选择。

我要指出,这可以在Firefox中被覆盖,可能有一些其他的浏览器。 (但因为它不是标准的,它不能帮助你)

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"
    });

解决方案

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)

这篇关于jQuery的:使同步Ajax请求,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆