使用 jquery 的多个 ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4680232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:43:05  来源:igfitidea点击:

multiple ajax requests with jquery

jqueryajax

提问by Emil

I got problems with the async nature of Javascript / JQuery.

我遇到了 Javascript / JQuery 的异步性质的问题。

Lets say the following (no latency is counted for, in order to not make it so troublesome);

让我们说以下(不计算延迟,以免让它变得如此麻烦);

I got three buttons (A, B, C) on a page, each of the buttons adds an item into a shopping cart with one ajax-request each. If I put an intentional delay of 5 seconds in the serverside script (PHP) and pushes the buttons with 1 second apart, I want the result to be the following:

我在一个页面上有三个按钮(A、B、C),每个按钮将一个项目添加到购物车中,每个按钮都有一个 ajax 请求。如果我在服务器端脚本 (PHP) 中故意延迟 5 秒并按 1 秒间隔按下按钮,我希望结果如下:

Request A, 5 seconds
Request B, 6 seconds
Request C, 7 seconds

However, the result is like this

然而,结果是这样的

Request A, 5 seconds
Request B, 10 seconds
Request C, 15 seconds

This have to mean that the requests are queued and not run simultaneously, right? Isnt this opposite to what async is? I also tried to add a random get-parameter to the url in order to force some uniqueness to the request, no luck though.

这必须意味着请求已排队并且不会同时运行,对吗?这不是与异步相反吗?我还尝试向 url 添加一个随机的 get-parameter 以强制请求具有某些唯一性,但没有运气。

I did read a little about this. If you avoid using the same "request object (?)" this problem wont occure. Is it possible to force this behaviour in JQuery?

我确实阅读了一些关于此的内容。如果您避免使用相同的“请求对象(?)”,则不会出现此问题。是否可以在 JQuery 中强制这种行为?

This is the code that I am using

这是我正在使用的代码

 $.ajax(
 {
  url   : strAjaxUrl + '?random=' + Math.floor(Math.random()*9999999999),
  data  : 'ajax=add-to-cart&product=' + product,
  type  : 'GET',
  success  : function(responseData)
  {
   // update ui
  },
  error  : function(responseData)
  {
   // show error
  }
 });

I also tried both GET and POST, no difference.

我也尝试了 GET 和 POST,没有区别。

I want the requests to be sent right when the button is clicked, not when the previous request is finnished. I want the requests to be run simultaneously, not in a queue.

我希望在单击按钮时立即发送请求,而不是在前一个请求完成时发送。我希望请求同时运行,而不是在队列中。

采纳答案by Amir Raminfar

Ok here is what I would try, I think you need a better way to log the time. I am not sure what you are doing now to test the times but here is a good way.

好的,这就是我要尝试的方法,我认为您需要一种更好的方法来记录时间。我不确定您现在正在做什么来测试时间,但这是一个好方法。

Get Firebug for Firefox if you haven't already done so.

如果您还没有这样做,请获取 Firebug for Firefox。

Open Firebug and go to the net tab. Make sure you enable it.

打开 Firebug 并转到网络选项卡。确保启用它。

As you click each button you should see each request coming in. If you click each button, you don't see the next request starting until the previous request has ended then this tells me there is something wrong in javascript. However, I am guessing you should the beginning of each bar to be 1 second apart. Do this and let us know what you see.

当您单击每个按钮时,您应该会看到每个请求进入。如果您单击每个按钮,您将不会看到下一个请求开始,直到前一个请求结束,这告诉我 javascript 中存在问题。但是,我猜您应该将每个小节的开头间隔 1 秒。这样做,让我们知道你看到了什么。

Edit

编辑

I think I found you problem. I am guessing you are using sleep function in php. The sleep function sleeps for the current session. If you open up 3 tabs, and put the ajax url, you will notice the same behavior. 5, 10, then 15s for each tab to finish. I tried googling and found the same results with other people. This is probably because PHP is not really multithreaded. I suggest using a different framework.

我想我发现你的问题。我猜你在 php 中使用 sleep 函数。sleep 函数为当前会话休眠。如果您打开 3 个选项卡,并放置 ajax 网址,您会注意到相同的行为。5、10,然后每个选项卡完成 15 秒。我尝试谷歌搜索并发现与其他人相同的结果。这可能是因为 PHP 并不是真正的多线程。我建议使用不同的框架。

回答by Emil

Apparently this behaviour is very easily passable by using session_write_close(); in PHP. However, this opens up for DDos attacks with ease. I refreshed a page by holding down F5 in 10 seconds and killed my developmen computer in no-time.

显然这种行为很容易通过使用 session_write_close(); 在 PHP 中。但是,这为 DDos 攻击打开了方便之门。我通过在 10 秒内按住 F5 刷新页面并立即杀死了我的开发人员计算机。

Case closed.

案件结案。

回答by regilero

From what I understand Parallel ajax calls can be done only if you're not in different scope of js. Seems that being in different same js scope makes you using the same 'request object' in a serialize way.

据我了解,只有当您不在 js 的不同范围内时,才能进行并行 ajax 调用。似乎在不同的相同 js 范围内使您以序列化方式使用相同的“请求对象”。

So we can find on Stack overflow two excellent response on this at the page linked, see the second response with a parallel query stack object, this is the thing you need, add your requests on this parallele stack and they will really get fired in parallel (Stack overflow is the best site in the web :-) ). Parallel asynchronous Ajax requests using jQuery

所以我们可以在堆栈溢出上找到两个很好的响应,在链接的页面上,看到带有并行查询堆栈对象的第二个响应,这就是你需要的东西,在这个并行堆栈上添加你的请求,它们真的会被并行触发(堆栈溢出是网络中最好的站点 :-))。 使用 jQuery 的并行异步 Ajax 请求

回答by rcravens

There is a limit on the number of concurrent Ajax requests. See this

并发 Ajax 请求的数量是有限制的。看到这个

How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

流行浏览器中允许多少并发 AJAX (XmlHttpRequest) 请求?

Looks like it is browser dependent.

看起来它依赖于浏览器。

Bob

鲍勃

回答by Madman

you cant do it simultaneously, cause JS have fake async operations due to one browser UI thread. But you can try Web Workers, the way to execute code outsude of the browser UI thread. They already supported in Firefox 3.5, Chrome 3, and Safari 4. Maybe it will improve your application usability

你不能同时做这件事,因为 JS 由于一个浏览器 UI 线程而有虚假的异步操作。但是您可以尝试 Web Workers,这种在浏览器 UI 线程之外执行代码的方式。它们已经在 Firefox 3.5、Chrome 3 和 Safari 4 中得到支持。也许它会提高您的应用程序可用性