local-host 2009-01-15 12:45 采纳率: 100%
浏览 383

使用 jQuery 中止 Ajax 请求

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


  • 写回答

18条回答 默认 最新

  • Memor.の 2009-01-15 12:56

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

    See the documentation:

    • 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

    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).

    UPDATE 2: As of jQuery 3, the ajax method now returns a promise with extra methods (like abort), so the above code still works, though the object being returned is not an xhr any more. See the 3.0 blog here.

    UPDATE 3: xhr.abort() still works on jQuery 3.x. Don't assume the update 2 is correct. More info on jQuery Github repository.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 Qt4代码实现下面的界面
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码