简体   繁体   English

什么是管理多个ajax请求的正确方法?

[英]what is the right way to manage multiple ajax requests?

We've all seen some examples in AJAX tutorials where some data is sent. 我们都已经在AJAX教程中看到了一些示例,这些示例发送了一些数据。 They all (more or less) look like: 它们全部(或多或少)看起来像:

var http = createRequestObject(); // shared between printResult() and doAjax()

function createRequestObject() { /* if FF/Safari/Chrome/IE ... */ ... }

function printResult() 
{ 
    if (http.readyState == 4) { ... } 
}

function doAjax() {
    var request = 'SomeURL';
    http.open('post', request);
    http.onreadystatechange = printResult;
    data = ...; // fill in the data
    http.send(data);
}

// trigger doAjax() from HTML code, by pressing some button

Here is the scenario I don't understand completely: what if the button is being pressed several times very fast? 这是我不完全了解的情况:如果快速按几次按钮该怎么办? Should doAjax() somehow re-initialize the http object? doAjax()是否应该以某种方式重新初始化http对象? And if if the object is re-initialized, what happens with the requests that are being already on air? 如果对象被重新初始化,那么已经在播出的请求又会如何呢?

PS: to moderator: this question is probably more community-wiki related. PS:主持人:这个问题可能与社区维基有关。 As stated here ( https://meta.stackexchange.com/questions/67581/community-wiki-checkbox-missing-in-action ) - if I've got it right - please mark this question appropriately. 如此处所述( https://meta.stackexchange.com/questions/67581/community-wiki-checkbox-missing-in-action)-如果我理解正确-请适当地标记此问题。

Since AJAX has asynchronus nature, with each button click you would raise async event that would GET/POST some data FROM/TO server. 由于AJAX具有异步特性,因此每次单击按钮都会引发一个异步事件,该事件将在FROM / TO服务器中获取/发布一些数据。 You provide one callback, so it would be triggered as many times as server finishes processing data. 您提供了一个回调,因此它将在服务器完成数据处理后被触发多次。

It is normal behaviour by default, you should not reinitialize of http object. 默认情况下,这是正常行为,您不应重新初始化http对象。 If you want to present multiple send operation you have to do that manually (eg disabling button as first call being made). 如果要呈现多个发送操作,则必须手动执行此操作(例如,在进行首次呼叫时禁用按钮)。

I also suggest to use jQuery $.ajax because it incapsulate many of these details. 我还建议使用jQuery $ .ajax,因为它封装了许多这些详细信息。

Sure that numerous libraries exist nowadays that perform a decent job and should be used in production environment. 确保当今存在无数的图书馆,它们执行得体的工作,应在生产环境中使用。 However, my question was about the under-the-hood details. 但是,我的问题是关于引擎盖下的细节。 So here I've found the lamda-calculus-like way to have dedicated request objects per request. 所以在这里,我发现了类似lamda-calculus的方式,每个请求都有专用的请求对象。 Those object will obviously be passed to the callback function which is called when response arrives etc: 这些对象显然将传递给回调函数,当响应到达时调用该回调函数:

function printResult(http) {
  if (http.readyState == 4) { ... } 
  ...
}

function doAjax() {
    var http = createRequestObject();       
    var request = 'SomeURL';

    http.open('get', request);      
    http.onreadystatechange = function() { printResult(http); };
    http.send(null);
    return false; 
} 

Successfully tested under Chrome and IE9. 在Chrome和IE9下成功测试。

I've used a per-page request queue to deal with this scenario (to suppress duplicate requests and to ensure the sequential order of requests), but there may be a more standardized solution. 我使用了每页请求队列来处理这种情况(以抑制重复的请求并确保请求的顺序),但是可能会有更标准化的解决方案。

Since this is not provided by default, you would need to implement it in JavaScript within your page (or a linked script). 由于默认情况下未提供此功能,因此您需要在页面(或链接脚本)中的JavaScript中实现它。 Instead of starting an Ajax request, clicking a button would add a request to a queue. 单击按钮将向队列添加请求,而不是启动Ajax请求。 If the queue is empty, execute the Ajax request, with a callback that removes the queued entry and executes the next (if any). 如果队列为空,请执行Ajax请求,并使用回调删除队列中的条目并执行下一个条目(如果有)。

See also: How to implement an ajax request queue using jQuery 另请参阅: 如何使用jQuery实现ajax请求队列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM