Browsers stop a page on one site from reading responses from another site unless that other site says it's allowed. That permission is sent by the server as CORS headers, mainly Access-Control-Allow-Origin. A CORS error means the server didn't send the right headers for your site's address. So the fix belongs on the API's server: allow your exact origin (scheme, domain and port), and answer the browser's preflight OPTIONS request. Nothing in the front-end code can override it.
What's the problem
The browser console shows "Access to fetch at ... has been blocked by CORS policy" or "Cross-Origin Request Blocked", and your app can't load data from its API. It often works in local development but not in production, or vice versa.
Why it happens
- The API doesn't allow your origin, for example it allows
http://localhost:3000but nothttps://www.yoursite.com. wwwversus nowww, orhttpversushttps: each is a different origin.- The preflight
OPTIONSrequest fails (404, 401, or no CORS headers on it). - Credentials: cookies are sent, but the server uses
*as the allowed origin, which browsers reject with credentials. - The API errored and its error response had no CORS headers, so a 500 looks like a CORS error.
How to fix it
- Read the console message. It says which header is missing or wrong.
- Check the Network tab for the preflight
OPTIONSrequest and the real request, and their response headers. - On the API server, set
Access-Control-Allow-Originto your exact site origin (and include it on error responses too). - Allow the methods and headers you use (
Access-Control-Allow-Methods,Access-Control-Allow-Headers), and makeOPTIONSreturn success. - If you send cookies, set
Access-Control-Allow-Credentials: trueand a specific origin, not*. - Or avoid cross-origin calls by serving the API from the same domain as the site.
What Preventionlabs checks
One click runs the full report on your domain. For this problem it shows whether your site loads, how fast, and where its redirects go; every outside service your homepage loads scripts and files from. With Cloudflare connected, it also shows recent deploys and account changes, with their times. It doesn't call your API with your app's own requests or read your logs, so test the failing request directly as well.
Run your free reportYour first report is free, no card needed. Then A$50 a month, up to 10 domains. Read-only: it never changes anything.
Sources
- MDN Web Docs: Cross-Origin Resource Sharing (CORS)reference
Cross-Origin Resource Sharing ( CORS ) is an HTTP -header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.
- MDN Web Docs: CORS errorsreference
The only way to determine what specifically went wrong is to look at the browser's console for details.
- MDN Web Docs: Cross-Origin Resource Sharing (CORS)reference
Unlike simple requests , for "preflighted" requests the browser first sends an HTTP request using the OPTIONS method to the resource on the other origin, in order to determine if the actual request is safe to send.
- MDN Web Docs: Cross-Origin Resource Sharing (CORS)reference
The server must not specify the * wildcard for the Access-Control-Allow-Origin response-header value, but must instead specify an explicit origin;