在向React App中发送请求向Express Server发送请求时,如何解决CORS错误
当我在本地运行应用程序和服务器时,没有问题。但是,一旦我部署了Express服务器,每次提出请求时,都会收回以下错误。
“已被CORS策略所阻止:对飞行前请求的响应不会传递访问控制检查:在请求的资源上没有'访问控制 - 允许孔'标头。”
我的服务器正在使用CORS,我还尝试执行App.Options(*,CORS()),但这也行不通。我几乎尝试了在网上找到的所有解决方案。
这就是我在Express Server的索引中所拥有的。
app.options("*", cors());
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(cors());
app.get("/", (req, res) => {
res.send("Food Tracker Server");
});```
When I run both the app and the server locally, there is no issue. However, once I deploy the express server, every time I make a request, I get back the following error.
"has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."
My server is using cors, and I also tried doing app.options(*, cors()) but that won't work either. I have tried just about every solution online that I could find.
This is what I have in the index.js of the express server.
app.options("*", cors());
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(cors());
app.get("/", (req, res) => {
res.send("Food Tracker Server");
});```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样配置您的cors:
config your cors like this :