Goldbones Goldbones - 1 year ago 135
HTTP Question

No 'Access-Control-Allow-Origin' header is present on the requested resource - Ionic 2

I have a rest web service and now I want to make a post request from ionic 2 frontend app to authentication rest method.

On my login component I have:
(data) => this.handleSuccessAuthenticate(data),
(data) => this.handleErrorAuthenticate(data)

On my provider my _restClient code is:

public post(resource: string, data: Object, onSuccess: restClient, onError: callbackRestClient) {
var httpResult: Observable<Response>;

if (data === null) {
httpResult =, '{}', { headers: this.getHeaders() });
} else {
httpResult =, JSON.stringify(data), { headers: this.getHeaders() });

this.handleResult(httpResult, onSuccess, onError);

I also have a private method to set headers:

private getHeaders() {
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
headers.append("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");

return headers;

I have the classic message:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

What I´m doing wrong?

Answer Source

In fact, it's server-side issue and not an Angular2 one. The preflighted OPTIONS request need to return a Access-Control-Allow-Origin header in its response.

See these articles for more details:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download