[英]Can't resolve cors error in react and node app
I have just deployed a react/express app in Heroku which initially worked for a couple of minutes but then the dreaded cors error came up which I can never seem to fix.我刚刚在 Heroku 中部署了一个 react/express 应用程序,它最初工作了几分钟,但随后出现了可怕的 cors 错误,我似乎永远无法修复。 I've tried everything else suggested on other posts but nothing seems to work for me.
我已经尝试了其他帖子上建议的所有其他内容,但似乎对我没有任何帮助。
I've tried adding a proxy in my react app with:我尝试在我的反应应用程序中添加代理:
"proxy": "http://127.0.0.1:4000", (and also trying) ->
"proxy": "http://localhost:4000",
This didn't work so I tried adding the cors package in node which didn't work either.这不起作用,所以我尝试在节点中添加 cors package 也不起作用。 This is driving me crazy and I don't know what else I can try.
这让我发疯,我不知道我还能尝试什么。
const app = require("./app");
const cors = require("cors");
app.use(
cors({
allowedHeaders: ["authorization", "Content-Type"], // you can change the headers
exposedHeaders: ["authorization"], // you can change the headers
origin: "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
preflightContinue: false
});
);
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Listening on ${port}`);
});
I'm using axios for requests for example:我正在使用 axios 请求例如:
const allMoviesData = await axios.get("/movies?limit=12");
In chrome I get:在铬我得到:
GET http://localhost:4000/movies?limit=12 net::ERR_CONNECTION_REFUSED
(anonymous) @ bundle.js:31
bundle.js:31 Uncaught (in promise) Error: Network Error
at e.exports (bundle.js:31)
at XMLHttpRequest.d.onerror (bundle.js:31)
In firefox I get:在 firefox 我得到:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4000/movies?limit=12&searchBy=genres&filter=adventure.
Uncaught (in promise) Error: Network Error
exports http://localhost:3000/bundle.js:31
onerror http://localhost:3000/bundle.js:31
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4000/movies?limit=12. (Reason: CORS request did not succeed).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.