简体   繁体   English

多个ajax调用处理

[英]Multiple ajax call handling

I am using jQuery to make some ajax calls and wonder how people handle this situation. 我正在使用jQuery进行一些ajax调用,并想知道人们如何处理这种情况。

  1. An ajax request is made to the server to retrieve some info. 向服务器发出ajax请求以检索某些信息。
  2. Before the request returns another request is made. 在请求返回之前,会发出另一个请求。 The first request is now invalid and out of date. 第一个请求现在无效且已过期。

How do I tell that the initial request is now invalid and can be ignored when it returns. 如何判断初始请求现在无效,返回时可以忽略。 I only want to display the results of the second request and ignore (or cancel) the first. 我只想显示第二个请求的结果并忽略(或取消)第一个请求。

jQuery returns the XmlHttpRequest object from the call to ajax() , which I've used to achieve what you desire as follows: jQuery从调用ajax()返回XmlHttpRequest对象,我用它来实现你想要的,如下所示:

var lastRequest;
function getSomeData() {
    if(lastRequest) {
        lastRequest.abort();
        lastRequest = null
    }
    lastRequest = $.ajax(...);
}

The net effect is that earlier requests' responses are ignored if a subsequent request is made. 实际效果是,如果发出后续请求,则会忽略先前请求的响应。

Keep a record of a variable (eg:"request_id") that identify the request. 保留标识请求的变量(例如:“request_id”)的记录。 Add 1 to the variable at each new request. 在每个新请求中为变量添加1。 Only process the request if the request_id returned by the server is equal to the variable you have on the client. 仅当服务器返回的request_id等于客户端上的变量时才处理请求。

I've never been in this situation before, but you could send over a key that you increment when making the request and have the key sent back with the response. 我之前从未遇到过这种情况,但您可以发送一个在发出请求时递增的密钥,并将密钥与响应一起发回。 When the response arrives, you could then check the key to see if it is what you expected. 当响应到达时,您可以检查密钥以查看它是否符合您的预期。

var incrementor = 1;
var lastSent = 0;

jQuery(document).ready(function() {

    jQuery('a.submitter').click(function(event) {
        event.preventDefault();
        lastSent = incrementor;
        incrementor++;
        jQuery.post(
            'some-url.php',
            {
                'request-id': lastSent,
                'other-data': 'some-data'
            },
            function( data, textStatus ) {
                if( data.requestId == lastSent ) {
                    // Do stuff
                }
            },
            'json'
        );
    });

});

"How do I tell that the initial request is now invalid" “我如何判断初始请求现在无效”

You DON'T...! 你不...... You queue up Ajax Requests at the client layer and don't let them fire before the previous ones have returned and finished up their manipulation of the DOM... 您在客户端层排队Ajax请求,并且在先前的返回完成对DOM的操作之前不要让它们激活...

You can get some details about how to do this at my blog about "how to create an Ajax library" at; 您可以在我的博客上获取有关如何执行此操作的一些详细信息:“如何创建Ajax库”; http://ra-ajax.org/how-to-create-an-ajax-library-part-7-the-ra-ajax-class.blog http://ra-ajax.org/how-to-create-an-ajax-library-part-7-the-ra-ajax-class.blog

I prefer not to involve the service call, you may not always have control over the service definintion. 我不想涉及服务电话,您可能无法始终控制服务定义。 I would like to see something like this 我想看到这样的事情

$(function() {
    $('.ajaxButton').click(function() {
        $.currentCallId = (new Date()).getTime();

        $.ajax({
            type: "get",
            url: 'http://www.google.com/',
            beforeSend: function(xhr) {
                this.callId = $.currentCallId;
            },
            success: function(data) {
                if (this.callId === $.currentCallId) {
                    // process it
                } else {
                    // throw it out
                }
            }
        });
    });
});

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

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