如何从Angular2应用程序发送http post请求时启用CORS

时间:2017-02-02 10:03:00

标签: angular firebase xmlhttprequest cors

当我尝试发送http请求时,我收到了有关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.

然后我将标题设置为我的http请求。

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);

但我仍然有同样的错误,这不是解决问题的正确方法吗?

1 个答案:

答案 0 :(得分:2)

正如GünterZöchbauer评论的那样,

  

需要在服务器上配置CORS

以下链接可帮助您开始使用Enable CORS