2

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?

taku845
  • 133
  • 1
  • 3
  • 11
  • 2
    CORS needs to be configured on the server. Angular is not involved in that. – Günter Zöchbauer Feb 02 '17 at 10:03
  • You can use proxy to handle it on client side. https://webpack.github.io/docs/webpack-dev-server.html#proxy – dev_in_progress Feb 02 '17 at 10:36
  • Thank you for answering me soon. You mean I can handle this error by correctly setting proxy like this, right? [link](http://stackoverflow.com/questions/37172928/angular-cli-server-how-to-proxy-api-requests-to-another-server) – taku845 Feb 03 '17 at 07:30

1 Answers1

3

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.

shammelburg
  • 6,974
  • 7
  • 26
  • 34