mardi 1 octobre 2019

window.onbeforeunload is not calling service every time. In angularjs / javascript

I am working on an single page application, developed using angualrjs, web api. I want to logout user when he closes browser or tab. I used

I tried below code, whenever i debug the code it hits the logout url and updates user login status. But when I close the debugger, many times it doesnot hit logout URL.

$window.onbeforeunload = function (e) {     

        if ($rootScope.UserId) {
            var _requestParameter =
                {
                    username: $rootScope.UserId,
                    token: $rootScope.PrimaryIdentityToken
                }

            $http.post($rootScope.URLS.LogoutURL, _requestParameter);
        }

    };
    window.onbeforeunload = function(event)
    {
        var tmp = true;
        console.log('trying to Logout : ' + _requestParameter.username);
        if ($rootScope.UserId) {
            var _requestParameter =
                {
                    username: $rootScope.UserId,
                    token: $rootScope.PrimaryIdentityToken
                }

            $http.post($rootScope.URLS.LogoutURL, _requestParameter);

        }
    };

Help me to call the logout api everytime.




Aucun commentaire:

Enregistrer un commentaire