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

时间:2019-02-09 06:08:02

标签: json angular

 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;
      }
    });
  }

返回的内容https://i.imgur.com/Km8X2CX.png 导致存储https://i.imgur.com/FfsOTb5.png

我要存储令牌的值

将其更改为

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

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

2 个答案:

答案 0 :(得分:2)

您应该改为设置success.success.token。

更好地将响应重命名为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;
      }
    });
  }

答案 1 :(得分:0)

您在响应对象上有不必要的包装。删除它或使用response.success.token来获取令牌字符串。

您的代码变为:

output_x |= (((INPUT_1 | INPUT_2 | INPUT_3 | ... ) != 0) << 15);