Reputation: 1191
I'm facing some problems to perform a HTTP get operation with a server, within my Ionic 4 application. This is the method that I use to perform the HTTP request:
getRank(){
let headers = { headers: this.authenticationService.getHeadersToken(), withCredentials : true};
return this.http.get<RankRowsResponse>(this.env.API_URL + 'classificajson.php', headers);
}
And the method that returns the headers token is:
getHeadersToken(){
var headers = new HttpHeaders();
headers.append('Access-Control-Allow-Origin' , '*');
headers.append('Access-Control-Allow-Credentials', "true");
headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
headers.append('Accept','application/json');
headers.append('Content-Type','application/json');
console.log('PHPSESSID='+this.token.toString());
headers.append('cookie','PHPSESSID='+this.token.toString());
return headers;
}
When I call the method getRank() Google Chrome prints out this error:
Access to XMLHttpRequest at 'https://www.fantacalciopizza.it/php/classificajson.php' from origin 'http://localhost:8102' has been blocked by CORS policy
I've installed a Chrome Extensions for CORS but the error is still present; how Can I do? Where am I wrong with the request?
Upvotes: 0
Views: 380
Reputation: 148
When i start i had the same error, i change a lot code on my ionic project, however it was only possible when the changes were made on the server-side.
Upvotes: 0
Reputation: 697
First, you need to understand what CORS means.
You can start by reading this: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Probably, the domain you are serving your angular app, and the domain you are requesting to are different.
In that case, you need your backend server to be configured as allowing to your angular domain to make requests.
So this is not a problem on Ionic or Angular, but this is more a problem about your backend configuration.
Upvotes: 1