0

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 Chromium and mobile Safari (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.

FWIW: I'm testing on Ubuntu linux 12.04.

Edited: to remove claim that it also was fixed for Firefox. It isn't.

Community
  • 1
  • 1
Peter V. Mørch
  • 13,830
  • 8
  • 69
  • 103
  • can't reproduce your problem, the alert is shown for both jquery versions under chrome Version 28.0.1500.95 m – collapsar Aug 09 '13 at 00:24
  • That is strange. I see a difference in behavior in all of (FF, Chrome, mobile safari). My Chromium is `Version 28.0.1500.71 Ubuntu 12.04 (28.0.1500.71-0ubuntu1.12.04.1)` - seen from chrome://chrome/ – Peter V. Mørch Aug 09 '13 at 00:27
  • ARRHH... Sorry, I now discover that FF still calls the error: callback with 2.0.2. But at least now Chrome and mobile safari do not. That is still true. – Peter V. Mørch Aug 09 '13 at 00:35
  • just checked again - chrome does report the error in both cases ... i'm on win 7, however ... still strange – collapsar Aug 09 '13 at 00:38

0 Answers0