提取请求返回“无访问控制”标题错误,但是我的CORS设置了适当的设置吗?
我知道这已经被问到无数时期,但我发现的解决方案都没有对我有用。
我有一个带有express的节点API连接到mongoDB数据库。
我正在尝试将数据返回到前端的反应,但是每当我向API内OPINT请求时,我都会收到以下错误:
Access to fetch at 'http://redacted.com' from origin 'localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我尝试了从以前的问题中找到的几种解决方案,但似乎没有解决方案为我工作。
我尝试了以下解决方案:
app.use(cors({origin: 'http://localhost:3000', methods: 'GET, POST', preflightContinue: false'}));
app.use(cors({origin: '*'}));
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin': 'http::/localhost:3000');
res.setHeader('Access-Control-Allow-Methods': 'GET, POST');
next();
app.options('*', cors());
似乎没有任何作用,并且继续抛出了这种交叉误差。
这是我对我的API的完整index.js:
let express = require('express');
let bodyParser = require('body-parser');
let mongoose = require('mongoose');
const cors = require('cors');
let apiRoutes = require('./api-routes/routes');
let app = express();
var port = process.env.PORT || 3001
app.use('/api', apiRoutes)
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cors({
origin: 'http://localhost:3000'
}))
mongoose.connect('mongodb://localhost:27017', { useNewUrlParser: true });
var db = mongoose.connection;
app.get('/', (req, res) => res.send('API is working!'));
app.listen(port, function() {
console.log("Running server on port 3001");
});
react中的提取请求:
const response = await fetch('http://localhost:3001/api/pcg/bgproutestate');
const data = await response.json();
console.log(data);
有人可以告诉我是否有什么问题和如何解决这个问题?我从来没有以前的API遇到这个问题...谢谢
I know this has been asked countless times on SO but none of the solutions I have found have worked for me.
I have a Node API connected to a MongoDB database with Express.
I am trying to return data to a React front-end but whenever I make a request to the API endopint I get the following error:
Access to fetch at 'http://redacted.com' from origin 'localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have tried several solutions found from previous questions around this being asked on SO but none of the solutions seem to be working for me.
I have tried the following solutions:
app.use(cors({origin: 'http://localhost:3000', methods: 'GET, POST', preflightContinue: false'}));
app.use(cors({origin: '*'}));
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin': 'http::/localhost:3000');
res.setHeader('Access-Control-Allow-Methods': 'GET, POST');
next();
app.options('*', cors());
Nothing seems to work and continues to throw this cross-origin error.
Here is my full index.js for my API:
let express = require('express');
let bodyParser = require('body-parser');
let mongoose = require('mongoose');
const cors = require('cors');
let apiRoutes = require('./api-routes/routes');
let app = express();
var port = process.env.PORT || 3001
app.use('/api', apiRoutes)
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cors({
origin: 'http://localhost:3000'
}))
mongoose.connect('mongodb://localhost:27017', { useNewUrlParser: true });
var db = mongoose.connection;
app.get('/', (req, res) => res.send('API is working!'));
app.listen(port, function() {
console.log("Running server on port 3001");
});
Fetch request in React:
const response = await fetch('http://localhost:3001/api/pcg/bgproutestate');
const data = await response.json();
console.log(data);
Is anyone able to please tell me what could be the issue and how to solve this? I've never had this issue with previous APIs... Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,一旦我发布了一个问题,我就找到了一个解决方案...
如果我移动以下代码..:
.. above此代码..:
..它采用CORS策略并允许获取数据。我认为在定义API的路线之前,需要定义CORS。希望这对别人有帮助!
Okay as soon as I posted the question I found a solution...
If I move the following code..:
..above this code..:
..It takes the cors policy and allows data to be fetched. I think the cors needs to be defined before you define the routes of your API. Hope this helps someone else!