[英]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.