同时AJAX调用导致的随机误差误差、AJAX

2023-09-11 00:52:40 作者:泪纱

我有以下几点:

myfunc = function(url, callback, etc...) {
  $.ajax({
    type: 'GET',
    url: url,
    etc.});
}

function changeDivThis(data) {
  $('#this').html(data);
}

function changeDivThat(data) {
  $('#that').html(data);
}

和我打电话了ajax get函数两次连续:

and I call the ajax get function twice in succession:

myfunc('/getData?mode=THIS', changeDivThis, etc...);
myfunc('/getData?mode=THAT', changeDivThat, etc...);

和的结果是,在一些时间显著部分,说的20%的时间,id为股利='这个'将得到填充被调用为返回的数据。当我看到应用程序服务器日志,我看到了一些意想不到的东西,比如只有一个请求。

and the result is that some significant fraction of the time, say 20% of the time, the div with id='this' will get populated with the data that was returned by the call for THAT. And when I look at the app server logs, I see some unexpected stuff, like only one request.

当我在Firebug运行时,错误永远不会发生,一切都很好。我注意到,萤火虫大大减慢浏览器的操作,所以我假定这是一个线索的事业,有得做并发,如从服务器的第二个电话会返回第一个在某些场合,并在浏览器认为这是应对第一个电话。是否正确?

When I run in Firebug, the bug never occurs, everything is fine. I noticed that Firebug considerably slows down browser operation, so I'm assuming this is a clue to the cause, having something to do with concurrency, e.g. the second call returns from the server first on some occassions, and the browser thinks it's the response to the first call. Correct?

因此​​,在我与AJAX有限的经验,我认为它支持扔在服务器上的几个并发请求,然后将其整理出来以任何顺序返回的响应。这个问题使我相信,AJAX(或我的Firefox版本= 3.6.24 / jQuery的= 1.4.4)看到两个请求相同的URL一样,即使他们都有不同的参数。

So in my limited experience with AJAX, I thought it supported throwing several concurrent requests at the server and then it sorted out the responses in whatever order they returned. This problem leads me to believe that AJAX (or my version of Firefox = 3.6.24/jQuery = 1.4.4) sees two requests to the same url as the same even though they both have different parameters.

我看到了谷歌这个高度位居页: http://blogger.forgottenskies.com/? P = 173 这使我相信我不是一个人在遇到此问题。

I saw this highly-ranked page on Google: http://blogger.forgottenskies.com/?p=173 which makes me believe I'm not alone in experiencing this issue.

感谢您的任何见解。

推荐答案

有些浏览器,对可以进行的并发请求的数量进行了限制。你可能会想读这个线程How许多并发AJAX(XmlHtt prequest)请求允许在流行的浏览器?

Some browsers, have a limit on the number of simultaneous requests that can be made. You might want to read this thread How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

在过去,我已经使用了队列,以限制我的code发送的并发请求数。

In the past I've used a queue to limit the number of simultaneous requests that my code sends.