如何取消/放弃jQuery的AJAX请求? [英] How to cancel/abort jQuery AJAX request?

查看:830
本文介绍了如何取消/放弃jQuery的AJAX请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将进行每5秒一个AJAX请求。但问题是AJAX请求之前,如果previous请求没有完成我已经放弃了要求,作出了新的要求。

我的code是这样的事情,如何解决这个问题呢?

  $(文件)。就绪(
    变种FN =功能(){
        $阿贾克斯({
            网址:AJAX / progress.ftl',
            成功:功能(数据){
                //做一点事
            }
        });
    };

    VAR间隔= setInterval的(FN,500);
);
 

解决方案

jQuery的AJAX方法返回一个 XMLHtt prequest 对象。你可以使用这个对象来取消请求。

在XMLHtt prequest有href="http://www.w3.org/TR/XMLHtt$p$pquest/#the-abort-method">中止方法的 注意:如果请求已经被发送到服务器,然后服务器将处理该请求,即使我们放弃请求,但客户不会等待/处理响应

XHR对象还包含一个 readyState的这包含请求的状态(UNSENT-0,OPENED-1,HEADERS_RECEIVED-2,载荷-3和DONE-4)。我们可以用它来检查previous请求是否已经完成。

  $(文件)。就绪(
    VAR XHR;

    变种FN =功能(){
        如果(XHR&安培;&安培;!xhr.readystate = 4){
            xhr.abort();
        }
        XHR = $阿贾克斯({
            网址:AJAX / progress.ftl',
            成功:功能(数据){
                //做一点事
            }
        });
    };

    VAR间隔= setInterval的(FN,500);
);
 

JQUERY 1.5更新

由于jQuery的1.5 $。阿贾克斯函数现在返回 jqXHR 对象。它仍然提供了许多老的 XMLHtt prequest 向后兼容特性,但似乎利用了的readyState 属性,该属性打破兼容性!但无论如何,code以上需要改变使用的readyState 让它继续工作:

  $(文件)。就绪(
    VAR XHR;

    变种FN =功能(){
        如果(XHR&安培;&安培;!xhr.readyState = 4){
            xhr.abort();
        }
        XHR = $阿贾克斯({
            网址:AJAX / progress.ftl',
            成功:功能(数据){
                //做一点事
            }
        });
    };

    VAR间隔= setInterval的(FN,500);
);
 

I've an AJAX request which will be made every 5 seconds. But the problem is before the AJAX request if the previous request is not completed I've to abort that request and make a new request.

My code is something like this, how to resolve this issue?

$(document).ready(
    var fn = function(){
        $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };

    var interval = setInterval(fn, 500);
);

解决方案

The jquery ajax method returns a XMLHttpRequest object. You can use this object to cancel the request.

The XMLHttpRequest has a abort method, which cancels the request.
Note: If the request has already been sent to the server then the server will process the request even if we abort the request but the client will not wait for/handle the response.

The xhr object also contains a readystate which contains the state of the request(UNSENT-0, OPENED-1, HEADERS_RECEIVED-2, LOADING-3 and DONE-4). we can use this to check whether the previous request was completed.

$(document).ready(
    var xhr;

    var fn = function(){
        if(xhr && xhr.readystate != 4){
            xhr.abort();
        }
        xhr = $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };

    var interval = setInterval(fn, 500);
);

JQUERY 1.5 UPDATE

Since jQuery 1.5 the $.ajax function now returns a jqXHR object. It still provides many of the old XMLHttpRequest properties for backwards compatibility but seems to capitalise the readystate property which breaks compatibility! Anyway the code above needs to be changed to use readyState for it to continue functioning:

$(document).ready(
    var xhr;

    var fn = function(){
        if(xhr && xhr.readyState != 4){
            xhr.abort();
        }
        xhr = $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };

    var interval = setInterval(fn, 500);
);

这篇关于如何取消/放弃jQuery的AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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