I get this CORS error when I try to access a Netlify function (deployed to Netlify through GitHub) from my website (also deployed to Netlify). This is a simple contact function, send an email through the contact form. My domain is from Google Domains.
I use CORS in my server file. I also tried to put a netlify.toml file in the root directory and in the build directory too but still got the error. What should I do?
Hi, @dmihalovits.
Generally speaking, if someone is visiting your page at the custom domain (www.mygoogledomain.com) and you make an API call to the site subdomain under netlify.app (like quizzical-abcd-abcd123.netlify.app) then you are not using the same domain name and you are going to trigger CORS errors if there is a CORS policy in place (unless the CORS policy specifically allow this). If you want to learn more about CORS, there is a StackOverflow post here:
Also, the MDN documentation on CORS is amazingly well written (in my opinion at least):
In most cases, you can simply remove the domain from the URL leaving the path only. This will cause the axios API call to target the same domain the page is already using.
That should automatically use the correct domain name. If that doesn’t work, please let us know. Also, if you send is a link to the actual site (or tell us the API ID for the site), we can take a look at the headers you are sending.