Aborting Ajax Requests with jQuery
Can jQuery be used to cancel an Ajax request before receiving the response?
Answer:
Yes, it is possible to abort an Ajax request in jQuery using the abort() method. Most Ajax methods in jQuery return an XMLHttpRequest object (or its equivalent) that exposes the abort() method.
For example:
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();
Update:
In jQuery 1.5 and later, the returned object is a wrapper (jqXHR) for the native XMLHttpRequest object, which still exposes the abort() method.
Update 2:
In jQuery 3, the ajax method returns a promise that includes the abort method.
Update 3:
It should be noted that xhr.abort() continues to function in jQuery 3.x.
The above is the detailed content of Can jQuery Abort an Ajax Request Before Receiving a Response?. For more information, please follow other related articles on the PHP Chinese website!