简体   繁体   English

无法解决反应和节点应用程序中的 cors 错误

[英]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).

Any other suggestions would be appreciated.任何其他建议将不胜感激。 在此处输入图像描述

Check the request and response headers.检查请求和响应标头。

The bluish parts should be the same.蓝色的部分应该是一样的。

在此处输入图像描述

If they are not the same for you, that means whatever you are doing, isn't working like it should be.如果它们对你来说不一样,那就意味着无论你在做什么,都没有像它应该的那样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应节点快递应用程序 - Heroku CORS 错误 - React node express app - Heroku CORS error 节点js应用程序上的cors策略错误并做出反应 - Error for cors policy on node js app and react 找不到反应应用程序模块:错误:无法解析'../bugControler' - React app Module not found: Error: Can't resolve '../bugControler' 错误:无法在 React App 中解析“./components/Upload”和“./components/Results” - Error: Can't resolve './components/Upload' and './components/Results' in React App 找不到模块:错误:无法解析“反应” - Module not found: Error: Can't resolve 'react' 找不到模块:错误:无法从带有 TypeScript 的 React 18 解析“./App” - Module not found: Error: Can't resolve './App' from React 18 w/TypeScript react-boilerplate 错误:无法解析“/Users/react-boilerplate/node_modules/xlsx-style”中的“../xlsx” - react-boilerplate Error: Can't resolve '../xlsx' in '/Users/react-boilerplate/node_modules/xlsx-style' 无法通过 CORS 请求错误与 Phonegap App 中的服务器连接 - Can't connect with server in Phonegap App by CORS request error 反应:找不到模块:错误:无法解析“反应/库/更新” - React: Module not found: Error: Can't resolve 'react/lib/update' React 运行错误无法解析 react-dom/client - React Run Error Can't resolve react-dom/client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM