Detecting an Ajax Request Interrupted by a User Action

I want to identify cases when an Ajax call is made and then it is interrupted due to some user action (for example, the user goes to another page)?

I see that when this happens, my onreadystatechange handler is called using readyState == 4 (DONE) and status == 0.

The question is, is there a way to make sure that it really matches the described scenario (the request is interrupted due to user action)? I want to avoid masking other potentially more dangerous errors.

+3
source share
2 answers

For a user navigating to another page, you can use the event onbeforeunload.

For instance:

window.onbeforeunload = function() {
    // Check ajax request state and do something accordingly...
}
+3
source

(boolean), ajax. . , .

+1

All Articles