Reputation: 25
I am working with Ionic, I need to send token to get the api data. The below code works fine
this.hdrs=new HttpHeaders({
Authorization:"Token "+'d5213668d34013c9087273a5efab6d39d6d8bfb9'
})
but when tried to read data from local storage, not getting the api data. Showing authorization error. The code i tried is
this.hdrs=new HttpHeaders({
Authorization:"Token "+(localStorage.getItem('token')
})
when console.log((localStorage.getItem('token'))) is tried printing the same token - "d5213668d34013c9087273a5efab6d39d6d8bfb9" Checked the type of both Both are giving string
Upvotes: 2
Views: 462
Reputation: 189
Just Put in API rather than Local
//get code //api code (header value, where getapI CALLED
this.headers = {
Authorization: "Token " + this.getLoginCred(),
"Content-Type": "application/json; charset=utf-8",
Accept: "application/json",
};
//my getLoginCred() function is as follows:
var data = localStorage.getItem('LoginData');
if (data != null && data != '' && data != undefined) {
return JSON.parse(window.atob(data.split('.')[1]));
}
else {
return null;
}
I Do this code and my code runned Perfectly. WITHOUT ANY ERROR OR WARNING..
Upvotes: 1
Reputation: 313
You're last code snippet above is missing a closing )
this.hdrs=new HttpHeaders({
Authorization:"Token "+(localStorage.getItem('token'))
})
...Definitely still check out Brian's comment!
Upvotes: 0