【问题标题】:Failed to fetch data from Heroku app. CORS error无法从 Heroku 应用获取数据。 CORS 错误
【发布时间】:2018-12-04 09:32:45
【问题描述】:

我一直在努力从 Redux 中的 heroku 应用程序获取数据,并且遇到了几个 CORS 错误:

Failed to load https://app-name.herokuapp.com/users: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

第二个错误

localhost/:1 Uncaught (in promise) TypeError: Failed to fetch

我从 Express 创建了 Heroku 应用,在我的响应代码中我有

res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);

我在 Redux 中的 fetch 函数看起来像

const url = "https://app-name.herokuapp.com/users";
return fetch(url, {
 method: 'GET',
 mode: 'cors',
 headers: { 'Content-Type': 'text' }
})

【问题讨论】:

    标签: express heroku redux cors fetch


    【解决方案1】:

    在您的 cors 设置中,您有 http://localhost:3000,但您的服务器在 https://app-name.herokuapp.com 上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 2021-10-18
      • 2018-03-26
      • 2012-11-04
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多