抓取API无法加载…没有“Access-Control-Allow-Origin”头

我有

  • 一个restapi部署在heroku上
  • 一个反应的应用程序部署在heroku上

反应的应用程序尝试使用whatwg-fetch从其余api获取数据。

var header = {"Content-Type": "multipart/form-data", 'Origin': 'https://foo.bar', 'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': 'X-Requested-With'}; var options = {method: 'GET', credentials: 'include', headers: header}; fetch('https://myrest.api/foo', options)... 

但是我不能得到任何数据,这是说的

 Fetch API cannot load https://foo.bar. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://foo.bar' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 

当我尝试使用curl来获取数据的时候,它可以工作

 curl -H "Origin: https://foo.bar" \ -H "Access-Control-Request-Method: GET" \ -H "Access-Control-Request-Headers: X-Requested-With" \ -X GET --verbose https://myrest.api/foo -D header.txt 

和响应头(来自curl命令)

 HTTP/1.1 200 Server: Cowboy Connection: keep-alive Access-Control-Allow-Origin: https://foo.bar Vary: Origin Access-Control-Allow-Credentials: true Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Fri, 09 Dec 2016 23:52:57 GMT Via: 1.1 vegur 

Cors通过@CrossOrigin在弹簧启动应用程序中启用

在此先感谢最好的问候

Solutions Collecting From Web of "抓取API无法加载…没有“Access-Control-Allow-Origin”头"