【发布时间】:2019-01-13 18:23:34
【问题描述】:
我的 React 应用程序出现此错误。 “无法加载 https://app-name.herokuapp.com/users/:请求的资源上不存在‘Access-Control-Allow-Origin’标头。因此,不允许访问源‘http://localhost:3000’。”
我的 Express 应用中的代码
var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, X-Auth-Token, Accept");
next();
}
我的 Redux 提取调用中的代码
return dispatch => {
const url = "https://app-name.herokuapp.com/users/";
return fetch(url, {
method: 'GET',
mode: 'cors',
})
.then(handleErrors)
.then(res => res.json())....
【问题讨论】:
标签: express heroku redux cors fetch