When a user clicks on a link, I want to initiate an AJAX request to save the current page's content and navigate away simultaneously.
Typically, when the window is attempting to navigate away, all AJAX requests are interrupted prematurely. This could mean that the server hasn't finished processing the request. If the AJAX call is aborted too soon, any changes made will not be saved.
The valid readystates according to W3Schools
1: server connection established
2: request received
3: request processing
4: request completed and response ready
Should I wait for state 2 or 3 to ensure the request goes through on major browsers before navigating away?
I understand the risk of not confirming a successful save in state 4, which means the user may not be aware of any failure in saving changes. However, considering the stable code, once the server acknowledges the request, it's highly likely that if the changes aren't saved, the user won't have any recourse anyway (like a deleted or locked post). Plus, the changes might not be crucial.
The main concern is handling Internet Connection Failures - at least being informed about them in major browsers.
Do I need to wait until state 4 to detect such failures?
If we ignore connection failures entirely, which state should I wait for?