13522

Why is ajax call in window.unonload never called?

Question:

I have an upload view written in Angularjs. I want to ensure that during an upload if the page is <strong>reloaded</strong> or <strong>closed</strong>; all files that uploaded to the server should be removed for consistency. So what I tried is as following:

// this function is under /upload view, and inside UploadController window.onunload = function () { $http.delete(...call delete on my server..); };

howevever, DELETE does not reach to server, what am I missing?

Answer1:

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.

<img alt="Save failing" class="b-lazy" data-src="https://i.stack.imgur.com/VM9Zs.png" data-original="https://i.stack.imgur.com/VM9Zs.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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.

Recommend

  • Extract audio frames from AMR-NB file
  • Bootstrap ScrollSpy skips section when no more scrollable area
  • No System image available when adding AVD in Android studio
  • How to get style in Elm
  • Adding Existing Group to Custom Ribbon in Office Word 2010
  • how to convert public key of an x509 certificate in JAVA to hex
  • How do I programatically open the Developer Tools' Console in a Firefox addon?
  • Opening links in a new tab and only the new tab
  • How do references in functions work?
  • How to avoid multiple definition of function (Linux, GCC/G++, Code::Blocks)
  • Reload UITableView from another tab
  • Load image without autoscaling in Android
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • How to add System.Windows dll to Visual Studio 2010 express?
  • Issue with routerLink directive
  • Outlines on links in IE9 remains when focus is changed
  • Application level floating views with navigation in Android
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • Button click event not firing in jQuery
  • Webgrid not refreshing after delete MVC
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Adding a button at the bottom of a table view
  • Display issues when we change from one jquery mobile page to another in firefox
  • Getting last autonumber in access
  • Different response to non-authenticated users and AJAX calls
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Delete MySQLi record without showing the id in the URL
  • GridView Sorting works once only
  • bootstrap to use multiple ng-app
  • Data Validation Drop Down Box Arrow Disappearing
  • How to get icons for entities from eclipse?
  • Why joiner is not used after Sequence generator or Update statergy
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • Turn off referential integrity in Derby? is it possible?
  • Is it possible to post an object from jquery to bottle.py?
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Why do underscore prefixed variables exist?
  • How to push additional view controllers onto NavigationController but keep the TabBar?