Error occurs when i try to post in get method to rest api. but in postman it working perfectly.
public cartview() {
let localaccesstoken = localStorage.getItem('token');
let apiUrl = this.urlService.apiUrl+'cart?access_token='+localaccesstoken;
console.log(apiUrl);
return new Promise((resolve, reject) => {
console.log("test : "+apiUrl);
this.http.get(apiUrl)
.subscribe(res => {
console.log("w3cert Url : "+JSON.stringify(res));
console.log(resolve(res));
}, (err) => {
console.log('w3cert : '+reject(err));
});
});
In console log shows an api. I copied that api and posted in postman it working. but in app it shows error
Error: “Uncaught (in promise): [object Object]”
AJ.M.A
March 29, 2019, 7:32am
2
how did you save your token in the local storage? Is it a string?
yes, i save a token in string
AJ.M.A
March 29, 2019, 9:10am
4
Did you save an object which you JSON.stringify()
? If you did I suggest that you should JSON.parse(localStorage.getItem(‘token’)).
When i tried that it shows : ERROR SyntaxError: “JSON.parse: unexpected character at line 1 column 1 of the JSON data”
AJ.M.A
March 29, 2019, 9:54am
6
Did you try to log the localaccesstoken
after getting the value in localstorage? If it shows your token I think we’re looking at a different problem here.
this is my log. in that token shows correctly
AJ.M.A
March 29, 2019, 10:09am
8
I see it’s not the token. Well I’m out of ideas but I suggest that you check your apiURL
maybe you missed something.
i try log for apiurl. and i copied that and posted in postman it work perfectly. only in app it shows this error
sisdev
March 29, 2019, 10:15am
10
since this is a promise error, i think you should try to return resolve and reject, to see if this solves it.
because everything else seems to be ok to me. What i miss in your promise is just returning resolve and reject.
Try converting your subscribe statement to promise before resolving.
http.get().toPromise().then((res) => {
resolve(res);
}).catch((err) =. {
reject(err);
})