0

I have been trying to call a a logout service from whitin onunload or onbeforeunload window events.

This is because I needed to logout upon closure of window or a tab. I set $window.onbeforeunload within the run() block.

$window.onbeforeunload worked fine when I wanted to show a warning to the user upon closure of tab or window. But, when I tried to call a logout function from within the onbeforeunload event, the function was not called.

I tried calling the $http from within run() block or calling it from a data service, but the result was the same.

How can I solve this?

Joubin Z.
  • 91
  • 1
  • 5
  • seems that the problem is that angularJS $http service is always asynchronous, while in onbeforeunload a synchronous call is required. This means that I needed to use a synchronous call written in javascript. Something like: ' var xhr = new XMLHttpRequest(); xhr.open("POST", "services/logout", false); // synchronous request xhr.send(null);' This I found from: [http://stackoverflow.com/questions/21689233/how-to-send-an-http-request-onbefor‌​eunload-in-angularjs](http://stackoverflow.com/questions/21689233/how-to-send-an-http-request-onbefor‌​eunload-in-angularjs) – Joubin Z. May 21 '15 at 09:29
  • Additional information about synchronous calls in javascript: [here](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Example_.231.3A_Automatic_logout_before_exit) – Joubin Z. May 21 '15 at 09:37

0 Answers0