Mar 23, 2011

Navigate away with a link before finish Ajax request (jQuery)

And it'll produce an error.

Anyway, there is a way to detect whether the failure comes from navigating away or other causes.


$.ajax({
type: "POST",
url: '/test',
cache: false,
data: {

},
success: function(data){

},
error: function(req, status, e){
if (req.status == 0) return;
alert('Cannot connect to the server. Please try again later.');
}
});


On the line 12 is the trick