错误如下:
XMLHttpRequest无法加载http://some_url.herokuapp.com/api/some_api/ . 请求的资源上不存在“Access-Control-Allow-Origin”标头 . 因此不允许Origin'http:// localhost:3000'访问 . 响应具有HTTP状态代码503 .
打电话的时候
return this._http.post(requestUrl,JSON.stringify(requestBody),requestOptions)
我过去曾遇到CORS(使用 Angular 1 时)的麻烦,我记得曾经CORS在服务器端被激活,我不得不 transform the http request to parse certain HTTP headers .
我很困惑它应该如何工作,所以任何解释都是非常受欢迎的 .
1 回答
实际上,它不是Angular2问题,而是服务器端的问题 . 预检的OPTIONS请求需要在其响应中返回
Access-Control-Allow-Origin
标头 .在原始请求中发送
Origin
标头将启用服务器端的CORS支持 . 当浏览器检测到请求的域名不是't the same as the caller'时,浏览器会自动添加此标头 .自 credentials aren't sent at this level 以来在服务器端实施预检OPTIONS请求时要小心 . 它们仅在目标请求中发送 . 这似乎是问题,因为你有503错误 . 你're trying to check if the OPTIONS request is authenticated but it'实际上并非如此......
有关详细信息,请参阅此文章: