0

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:

...this._restClient.post( 'authentication', body, (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 = this._http.post(this.getUrl(resource), '{}', { headers: this.getHeaders() }); } else { httpResult = this._http.post(this.getUrl(resource), 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?

1

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Is there anything I can do so we can bypass it at the client side because I don't own the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.