简体   繁体   English

"react、express、axios、node 和 mysql 的 CORS 问题"

[英]CORS issues with react, express, axios, node and mysql

I tried everything, adding all the headers and changing the options too but it seems that whatever I do, the cors error still haunts me.我尝试了所有方法,添加了所有标题并更改了选项,但似乎无论我做什么,cors 错误仍然困扰着我。

I get his error: CORS Missing Allow Origin我得到他的错误:CORS Missing Allow Origin

The frontend of my website is hosted on netlify and the backend on heroku.我网站的前端托管在 netlify 上,后端托管在 heroku 上。 I am using react, axios, mysql, node and express.我正在使用 react、axios、mysql、node 和 express。 I also haven't fully set up the domain name but the website works fine except for the cors error that happens only on certain post requests.我还没有完全设置域名,但该网站工作正常,除了仅在某些发布请求上发生的 cors 错误。 Suprisingly I think I got the preflight request working but it is also uncertain at times.令人惊讶的是,我认为我得到了预检请求,但有时也不确定。

here is my express node setup:这是我的快速节点设置:

 var cors = require('cors'); app.use(cors({ origin: true, credentials: true })); app.options('*', cors()) app.use(express.static("./public")); app.use(express.json()); app.use(express.urlencoded({ extended: true }));

Here is how my axios requests look like:这是我的 axios 请求的样子:

 Axios.post("https://backendurl" , { headers : { "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization", 'Content-Type': "application/json; charset=utf-8", 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Methods': "GET, POST, PUT, DELETE OPTIONS", 'Accept': '*', // Accept: '*', }, params: { name: name, } }).then((response) => { console.log(response.data); });

I also tried setting up a proxy through the allow all cors request server but it didn't work either.我还尝试通过允许所有 cors 请求服务器设置代理,但它也不起作用。

If anyone has any leads that would be of interest to solve this cors issue that would be great.如果有人有任何线索可以解决这个 cors 问题,那就太好了。 Especially concerning the main post request.特别是关于主要的帖子请求。 Thanks a lot.非常感谢。

What happens when you use the ip address instead?当您改用 IP 地址时会发生什么? So instead of https://backendurl , use https://192.xxx:port/因此,不要使用https://backendurl ,而是使用https://192.xxx:port/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM