【问题标题】:How to enable CORS when sending http post request from Angular2 app从Angular2应用程序发送http post请求时如何启用CORS
【发布时间】:2017-06-19 08:18:43
【问题描述】:

当我尝试发送 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);

但是我还是出现同样的错误,这不是正确的解决方法吗?

【问题讨论】:

  • 服务器需要配置CORS。 Angular 不参与其中。
  • 您可以在客户端使用代理来处理它。 webpack.github.io/docs/webpack-dev-server.html#proxy
  • 感谢您尽快回复我。你的意思是我可以通过正确设置这样的代理来处理这个错误,对吧? link

标签: angular firebase xmlhttprequest cors


【解决方案1】:

正如 Günter Zöchbauer 所说,

需要在服务器上配置CORS

这里有一个链接可以帮助您入门Enable CORS

【讨论】:

  • 谢谢!我会检查这个链接。
猜你喜欢
  • 1970-01-01
  • 2018-02-07
  • 2018-11-16
  • 2019-12-14
  • 2016-09-28
  • 2017-02-26
  • 2014-02-19
  • 1970-01-01
  • 2017-03-13
相关资源
最近更新 更多