How do I cancel an ajax request that I have not yet received the response from using jquery?
You can't recall the request but you can set a timeout value after which the response will be ignored. See this page for jquery AJAX options. I believe that your error callback will be called if the timeout period is exceeded. There is already a default timeout on every AJAX request.
It's an asynchronous request, meaning once it's sent it's out there.
In case your server is starting a very expensive operation due to the AJAX request, the best you can do is open your server to listen for cancel requests, and send a separate AJAX request notifying the server to stop whatever it's doing.
Otherwise, simply ignore the AJAX response.
Most of the jQuery ajax methods return an XMLHttpRequest (or ie equivalent) object, so you can just use abort()
msdn docs
MDC docs
var x = $.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
//kill the request
x.abort()
meouw's solution is correct, but if your are interested in more control then you could try the ajaxManager plugin for jQuery.