Seeking advice on a web application that needs to save user input data through an ajax call when they leave the site.
Currently using "Fetch" in a beforeunload event listener, but encountering issues with browsers cancelling the ajax call (specifically the preflight OPTIONS call being cancelled in Chrome's network panel).
Jquery's synchronous ajax call option is deprecated, and prompting users with a browser-based confirmation message via beforeUnload event is not ideal for business requirements.
Is there a solution to ensure the Ajax call goes through successfully?
Sample code snippet provided below:
window.addEventListener('beforeunload', beforeUnloadHandler);
beforeUnloadHandler(event) {
fetch(url, {
method: 'PUT',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Authorization: api.AUTHORIZATION_HEADER
},
body: JSON.stringify(data)
})
}