$.ajax seems to not call the error: callback in jQuery 2.x e.g. due to
page reloads. Fantastic! But why?
I've seen in jQuery 1.10.1 that if an $.ajax request is underway when a
page unload occurs (e.g. due to user clicking a link or reloading the
page), the error: callback is called.
That seems not be the case in jQuery 2.0.0 or 2.0.2. And that is fantastic.
There are 7 questions at least here on SO, that ask about error: being
called when navigating. E.g. handle ajax error when a user clicks refresh
See this jsfiddle - it starts out running 1.10.1 but you can change it to
2.0.2 in the controls on the left. Essentially:
$.ajax({
type: "POST",
url: "/echo/json/",
data: {
json: JSON.stringify({
a: 23
}),
delay: 30
},
cache: false,
success: function (json) {
$('#result').text(JSON.stringify(json));
},
error: function () {
alert("error callback called");
}
});
Notice how the alert is shown for 1.10.1 but not for 2.0.2.
However, I don't see anything in the "jQuery 2.0 Released" announcement to
that effect.
Can I rely on this being fixed/changed or is this an accident that could
be browser-dependent or something? It seems to have been fixed at least in
FF, Chromium and Safari on iPad.
I'm looking for a source of the fix, so I can see if I can get 1.x to stop
calling the error handler or at least detect in the error callback whether
this is due to user action / navigation. Especially because the best
solution I've seen so far is to use beforeunload to detect page unload at
the time of the error: callback. Problem is, that beforeunload doesn't
work in mobile safari.
No comments:
Post a Comment