Friday, January 28, 2011

How to cancel Ajax call in JQuery?

By abort method in XMLHttpReuest.
Basically, ajax call in JQuery is a XMLHttpReuest.

Var currentcall = null;
currentcall = $.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
currentcall=null;
}
});

In other event handler, you can call currentcall.abort();

Reference:
http://www.w3.org/TR/XMLHttpRequest/#the-abort-method
http://api.jquery.com/jQuery.ajax/