重新打开浏览器后丢失会话存储数据 [英] losing session storage data after reopen the browser

查看:582
本文介绍了重新打开浏览器后丢失会话存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行该功能后,在浏览器中创建会话。然后关闭并打开会话将清除的同一浏览器。

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;    

            });
    };


推荐答案

回答OP添加的评论。

是的,可以在角度控制器中的文档就绪上执行代码。

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

在您的基础中附加以下事件(main )controller:

Append following event in you base( main) controller:

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

会话存储:


页面会话结束时,会在sessionStorage中存储的数据被清除。只要浏览器处于打开状态,页面会话就会持续,并且会在页面重新加载和恢复后继续存在。 在新标签页或新窗口中打开页面会导致新会话启动,这与会话Cookie的工作方式不同。

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参考

编辑(回答Op对此答案的评论):

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

要在用户关闭浏览器后保持信息,您需要用户 localStorage

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

Google it,它与sessionStorage的使用类似,易于理解。找到这个 codepen 来帮助你。仍然阅读它,然后才使用它。

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.

此外,如果你开放讨论,我建议你只在 localStorage存储用户名而不是像logedIn这样的标志,而是在服务器上维护这些信息。然后,每当用户关闭一个系统上的浏览器并从其他系统登录并注销时。他将从他使用的所有系统中退出。这是大多数Web应用程序的可接受行为。

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.

这篇关于重新打开浏览器后丢失会话存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆