losing session storage data after reopen the brows

2019-08-10 20:45发布

after execute the function the session created in browser. and i close and open the same browser the session will cleared.

$scope.logIn = function () {        
        $http(
            {
                method: 'post',
                url: 'Service.asmx/fn_CheckLogin',
                data: $.param({ strMob: $scope.login.mob, strPwd: $scope.login.pwd }),
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data, status, headers, config) {                               
                    sessionStorage.user = data[0].UserName;                
                    sessionStorage.loggedIn = true;                                
            }).error(function (data, status, headers, config) {
                sessionStorage.user = '';                  
                    sessionStorage.loggedIn = false;    

            });
    };

2条回答
The star\"
2楼-- · 2019-08-10 20:55

Answer to comment added by OP.

Yes, it is possible to execute code on document ready in your angular controller.

Append following event in you base( main) controller:

angular.element(document).ready(function () {
   // code you need to execute on document ready
});

Session Storage:

Data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

MDN reference

Edit (answer to Op's comment on this answer):

To maintain information even after user closes browser, You need to user localStorage.

Google it, it is similar to sessionStorage in usage and is easy to understand. Found this codepen to help you maybe. Still read about it and only then use it.

Also if you are open for discussion, I will suggest you to store just the username in localStorage and not a flag like logedIn instead maintain this information on server . Then whenever user closes browser on one system and logs in from other system and logs out. He will be logged out from all systems he has used. This is accepted behavior for most of the web applications.

查看更多
再贱就再见
3楼-- · 2019-08-10 21:14

Use $localStorage. Here is the link: https://github.com/gsklee/ngStorage

查看更多
登录 后发表回答