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?

2
  • 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 Commented May 21, 2015 at 9:29
  • Additional information about synchronous calls in javascript: here Commented May 21, 2015 at 9:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.