中止使用jQuery Ajax请求 [英] Abort Ajax requests using jQuery

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

问题描述

使用jQuery,我怎么能取消/放弃一个Ajax请求,我还没有收到回应?

Using jQuery, how can I cancel/abort an Ajax request that I have not yet received the response from?

推荐答案

大多数的jQuery的Ajax方法返回一个XMLHtt prequest(或等值)对象,所以你可以用中止( )

Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort().

请参阅文档:

  • abort Method (MSDN). Cancels the current HTTP request.
  • abort() (MDN). If the request has been sent already, this method will abort the request.
var xhr = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});

//kill the request
xhr.abort()

更新: 在jQuery 1.5返回的对象是一个包装名为jqXHR本地XMLHtt prequest对象。这个对象似乎是揭露所有的天然属性和方法,所以上面的例子仍然有效。见的 的jqXHR对象 的(jQuery的API文档)。

UPDATE: As of jQuery 1.5 the returned object is a wrapper for the native XMLHttpRequest object called jqXHR. This object appears to expose all of the native properties and methods so the above example still works. See The jqXHR Object (jQuery API documentation).

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

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