Skip to content Skip to sidebar Skip to footer

Why Is Ajax Call In Window.unonload Never Called?

I have an upload view written in Angularjs. I want to ensure that during an upload if the page is reloaded or closed; all files that uploaded to the server should be removed for co

Solution 1:

I've had this issue before too. Open up the browser console and go to the network tab. Now navigate away from the page. You will see for a quick moment that your ajax request is marked as (canceled) because the browser cancels the request as you move away.

Save failing

I've observed this behaviour in google chrome and haven't checked other browsers, but if it doesn't work reliably in one of the most popular browsers then you know this isn't a reliable solution overall.

Basically, there is no reliable way to do this. I've found not even doing the ajax call in window.onbeforeunload is reliable.

If I were you, I would put an expiry date on your uploaded files and have a cron job that deletes them after the expiry date is reached. Even if window.onunload worked, it still wouldn't be a reliable solution—for example, when a user's machine unexpectedly shuts down.

Post a Comment for "Why Is Ajax Call In Window.unonload Never Called?"