Hi guys…
Having this awful CORs and Netlify issue we all been talking about
My site is: https://competent-mayer-42a09b.netlify.app/
I want to implement a basic contact database from a form to collect some user info. My solution works well locally, but when I deploy my app, I get this error:
Access to XMLHttpRequest at 'https://ometer-back.herokuapp.com/' from origin 'https://competent-mayer-42a09b.netlify.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
POST https://ometer-back.herokuapp.com/ net::ERR_FAILED 503
I have followed advice from this forum thread and this one … with no luck. I now have in the root of my ReactJS project this Netlify.toml configuration:
# The following redirect is intended for use with most SPAs that handle
# routing internally.
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[[headers]]
# Define which paths this specific [[headers]] block will cover.
for = "/*"
[headers.values]
Access-Control-Allow-Origin = "*"
I read about a headers file and a proxy that I might have to add?
But I have not done this before and it’s confusing to me, as I am just getting familiar with serving up the static build content of my React application.
On my React Axios post request I’ve included header info
await axios.post(process.env.REACT_APP_API_ENDPOINT,
{
crossdomain: true
},
{
headers: {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,PATCH,OPTIONS',
'Content-Type': 'application/x-www-form-urlencoded'
}
},
On my server I’ve included…
app.use(cors({
// origin: process.env.ORIGIN
origin: "*",
methods: ["GET", "POST"]
}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
all the relevant requirements are inserted earlier in my code too.
Please help me I don’t know what else to doo… D:
Thank you!