LocalStorage.getItem:检查密钥是否存在

时间:2018-03-05 11:46:07

标签: javascript jquery json ajax

首先我在用户登录login.html页面后设置字符串值:

success:function(data){
        if(res.IsSuccess){      
             localStorage.setItem("token",data.Response.AuthToken);
             window.location.href="/dashboard.html";
        }
 } Once this is success it is redirecting to dashboard

在信息中心页面上,我正在检索如下值:

var Authtoken=localStorage.getItem("token")

如何检查 Authtoken 值是否存在?

更多解释:

我存储了服务器响应中的localStorage值,因此在Serverend中,authtoken过期时间为10分钟。

  1. Login page 一旦用户点击 RememberMe ,当时我只能使用localstorage存储文本框值,以便下次无需登录时重定向到仪表板(BrowserAction Icon点击)

  2. 点击登录按钮后,我会存储authtoken localStorage.setItem("token",data.Response.AuthToken);

  3. 如果仪表板基于localStorage值,如何检查它是否存在?

1 个答案:

答案 0 :(得分:0)

你可以检查它,以确保你从中得到一些东西

var token = localStorage.getItem("token")
var data = token && JSON.parse(token)
相关问题