1

I have to call post service on browser close and notify user data. I got it working on Chrome and Internet Explorer but on Firefox 30 it is never called. The following is the code snippet, please let me know if any other solutions..

window.onunload = function() {
  $.ajax({
  url: "http://localhost:9200/doLogout",
  type: "POST",
  data: {type: 3},
  success: function (data, textStatus, jqXHR) {
    // ...
  },
  error: function (jqXHR, textStatus, errorThrown) {
    // ...
  }
});
nmaier
  • 32,336
  • 5
  • 63
  • 78
madhured
  • 443
  • 2
  • 8
  • 2
    `window.onunload` works well in FF, but browser doesn't wait your asynchronous task to be done, it just closes the page. – Teemu Jun 24 '14 at 06:01
  • possible duplicate of [Can beforeunload be used to send XmlHttpRequests reliably](http://stackoverflow.com/questions/15479103/can-beforeunload-be-used-to-send-xmlhttprequests-reliably) – nmaier Jun 24 '14 at 06:03
  • @nmaier I donot want it to run on before unload, i know it works in before unload, i want it to run after user confirm leaving the page – madhured Jun 24 '14 at 06:06
  • 1
    It doesn't really matter if you send from `unload` or `beforeunload`. The question I duped yours to answers if you can reliably send stuff at all when unloading (no). – nmaier Jun 24 '14 at 06:09
  • @nmaier before unload is calling every single time but not unload – madhured Jun 24 '14 at 06:12
  • 2
    That's more or less coincidence and nothing to rely upon. – nmaier Jun 24 '14 at 06:13

0 Answers0