Reputation: 996
I'm getting some info from API. All the postman calls are success. Please find the attachment below.
When I call the same API in my angular application, I'm getting below error from back-end.
Component file
ngOnInit() {
this.getOffers();
}
getOffers(){
this._offerService.getOffers()
.pipe(first())
.subscribe(
data => {
if (data.CODE == 401){
console.log("Session not valid");
}
this.offers = <Offers[]>data;
console.log("offers response ",data);
},
error => {
console.log("An Error Occurred ", error);
});
}
Service File :
getOffers(): Observable<any>{
const token = this.authService.getToken();
console.log("Offer to ", token);
let headerOptions = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer '+token
});
let options = { headers: headerOptions };
const url = environment.baseURL + 'promotions';
return this._http.get<any>(url, options)
.pipe(map(offers => {
return offers;
}));
}
What I'm doing wrong here ?
Upvotes: 0
Views: 770
Reputation: 820
This could be an allowed hosts problem, since angular sends requests from a different port of your address.
Let me explain better:
In backend code there could be an allowed hosts setting which allows or handles requests from 192.168.1.4 (which postman may use to send requests) but not 192.168.1.4:4200 (which angular uses to send requests)
And the response error may differ if handled by backend code
Upvotes: 2