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
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
you should set success.success.token instead.
Better to rename the response as 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.
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;
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.