简体   繁体   English

如何访问此json令牌的值以存储在本地存储中

[英]How to access the value of this json token to store in in local storage

 loginUser(user: any) {
    return this.http.post(this.loginUrl, user)
    .subscribe((success: any) => {
      if(success) {
        localStorage.setItem('access_token', success.token);
        localStorage.setItem('token', JSON.stringify(success.token));
        return true;
      }
    });
  }

What is returned https://i.imgur.com/Km8X2CX.png Result in storage https://i.imgur.com/FfsOTb5.png 返回的内容https://i.imgur.com/Km8X2CX.png存储结果https://i.imgur.com/FfsOTb5.png

I want to store the value of the token 我想存储令牌的值

Changing this to 更改为

localStorage.setItem('access_token', success);
localStorage.setItem('token', JSON.stringify(success));

Result - https://i.imgur.com/c9wMosF.png 结果-https: //i.imgur.com/c9wMosF.png

you should set success.success.token instead. 您应该改为设置success.success.token。

Better to rename the response as response 最好将响应重命名为response

loginUser(user: any) {
    return this.http.post(this.loginUrl, user)
    .subscribe((response: any) => {
      if(response) {
        localStorage.setItem('access_token', response.success.token);
        // localStorage.setItem('token', JSON.stringify(response.success.token)); probably not needed.
        return true;
      }
    });
  }

You have unnecessary wrapping over the response object. 您没有必要包装响应对象。 Remove that or use response.success.token to get to the token string. 删除该标记或使用response.success.token进入标记字符串。

your code becomes: 您的代码变为:

loginUser(user: any) {
    return this.http.post(this.loginUrl, user)
    .subscribe((response: any) => {
      if(response) {
        localStorage.setItem('access_token', response.success.token);
        return true;
      }
    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将JSON数据存储和检索到本地存储? - How to store and retrieve JSON data into local storage? 如何使用json创建数组并将其存储在本地存储中 - How to create an array and store in local storage using json SyntaxError:尝试存储在本地存储中时,Angular2中位置0处JSON中的意外令牌u - SyntaxError: Unexpected token u in JSON at position 0 in Angular2 when try to store in local storage 颤振安全存储如何存储不记名令牌? - flutter secure storage how to store bearer token? 需要将从后端(json)发送的Access令牌存储到localstorage并使用该令牌登录。 怎么样? - Need to Store a Access token send from backend (json) into localstorage and login using that token. How? 如何从本地存储角度存储来自Twitter Rest API到ng-repeat的json响应 - How to store json response from twitter Rest API to ng-repeat in angular from local storage 如何将反应状态存储到本地存储中? - How to store a react state into local storage? 如何在浏览器的本地存储中存储多个数据 - how to store multiple data in local storage of the browser 如何在本地存储中存储数组元素 - How to store a array element in local storage 如何在Local Storage中存储对象数组? - How to store an array of objects in Local Storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM