Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use <code>abort()</code>.</p> <p>See the documentation:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/ms535920%28VS.85%29.aspx" rel="noreferrer">abort Method</a> (<a href="http://en.wikipedia.org/wiki/Microsoft_Developer_Network" rel="noreferrer">MSDN</a>). Cancels the current HTTP request.</li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort" rel="noreferrer">abort()</a> (<a href="http://en.wikipedia.org/wiki/Mozilla_Developer_Network" rel="noreferrer">MDN</a>). If the request has been sent already, this method will abort the request.</li> </ul> <pre><code>var xhr = $.ajax({ type: "POST", url: "some.php", data: "name=John&amp;location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); //kill the request xhr.abort() </code></pre> <p><strong>UPDATE:</strong> 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 <em><a href="http://api.jquery.com/jQuery.ajax/#jqXHR" rel="noreferrer">The jqXHR Object</a></em> (jQuery API documentation).</p> <p><strong>UPDATE 2:</strong> 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 <code>xhr</code> any more. See the <a href="http://blog.jquery.com/2016/01/14/jquery-3-0-beta-released/" rel="noreferrer">3.0 blog here</a>. </p> <p><strong>UPDATE 3</strong>: <code>xhr.abort()</code> still works on jQuery 3.x. Don't assume the <em>update 2</em> is correct. <a href="https://github.com/jquery/jquery/issues/2084#issuecomment-308173243" rel="noreferrer">More info on jQuery Github repository</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload