jquery的多个Ajax请求 [英] multiple ajax requests with jquery

查看:128
本文介绍了jquery的多个Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题的JavaScript / JQuery的的异步性。

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

可以说以下(无延时计数,为了不让它那么麻烦);

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

我有一个页面上的三个按钮(A,B,C),每个按钮添加一个项目放入购物车带各一个AJAX请求。 如果我把5秒的故意拖延,在服务器端脚本(PHP)和推按钮1秒之余,我希望得到的结果是以下内容:

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:

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

这都意味着,请求排队,而不是同时运行,对吧?心不是这个相反的是什么异步的?我也试着随机GET参数添加到URL,以迫使一些独特的要求,没有运气虽然。

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.

我看过一些关于这一点。如果您避免使用同样的请求对象(?)这个问题不会occure。是否有可能迫使JQuery的这种行为?

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?

这是我现在用的是code

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

我也试过GET和POST,没有什么区别。

I also tried both GET and POST, no difference.

我想要的请求送到被点击右按钮时,而不是当previous请求finnished。我想的请求被同时运行,而不是在一个队列中。

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.

推荐答案

确定这里就是我会尝试,我认为你需要一个更好的方式来记录时间。我不知道你现在正在做测试的时候什么,但这里是一个很好的方式。

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,并去净标签。请确保您启用它。

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

当你点击每一个按钮,你会看到每个请求进来,如果你点击每个按钮,你看不到下一个请求开始,直到previous请求已经结束那么这告诉我有什么不对的JavaScript 。不过,我猜你应该每栏的开头为1秒间隔。做到这一点,让我们知道你看到了什么。

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.

修改

我想我找到了你的问题。我在PHP中使用睡眠功能的猜测。睡眠功能休眠当前会话。如果你打开​​了3个标签,并把AJAX网址,你会发现相同的行为。 5,10,则15秒的每个选项卡来完成。我试着在Google上搜寻,发现与其他人相同的结果。这可能是因为PHP是不是真的多线程的。我建议使用不同的框架。

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.

这篇关于jquery的多个Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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