taku845
taku845

Reputation: 133

How to enable CORS when sending http post request from Angular2 app

When I tried to send http request, I got this error about CORS.

XMLHttpRequest cannot load 'sever-domain'. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'my-firebsase-hosted-app-domain' is therefore not allowed access.

Then I set headers to my http request like this.

let headers = new Headers(
  {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/x-www-form-urlencoded'
   }
);
let options = new RequestOptions({ headers: headers });
this.postRequestSub = this._http.post(this.url, body, options);

But I still have the same error, isn't this a correct way to solve it?

Upvotes: 2

Views: 7686

Answers (1)

shammelburg
shammelburg

Reputation: 7308

As Günter Zöchbauer commented,

CORS needs to be configured on the server

Here is a link which will help you get started Enable CORS.

Upvotes: 3

Related Questions