请求的资源(express+react/redux)上不存在“Access Control Allow Origin”标头
我的React应用程序出现此错误。加载失败:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许“源”访问 我的Express应用程序中的代码请求的资源(express+react/redux)上不存在“Access Control Allow Origin”标头,express,heroku,redux,cors,fetch,Express,Heroku,Redux,Cors,Fetch,我的React应用程序出现此错误。加载失败:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许“源”访问 我的Express应用程序中的代码 var express = require('express'); var router = express.Router(); router.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*")
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())....
这是您的Web服务的一个问题,当您从一个域指向另一个域时会发生这种情况,就像我从localhost:3000指向localhost:3001,这些是不同的,您应该在Web服务中启用CORS以允许该请求
你必须想办法在你的Heroku应用程序中启用CORS,正如我所知,外部API调用在免费域中被阻止,但Heroku不知道。我找到了答案。我所要做的就是推我的heroku应用程序。我不知道它有自己的git回购。Facepalm。那么我的heroku中的端口是否也应该指向3001?因为这就是我的react应用程序中正在发生的事情。你应该在Heroku应用程序中允许CORS。也许这会有帮助: