Browsers only let a page read replies from a different address (a different domain, port, or http versus https) if that server says the page is allowed. In production your frontend and backend usually live at different addresses, and your backend isn't saying your frontend's address is allowed. The fix goes on the backend: allow your live frontend's exact address. Nothing you change in the frontend will fix it.
What's the problem
Pages load, but nothing that needs data works: logins fail, lists are empty, buttons do nothing. The browser's console shows a red message mentioning "CORS", "Access-Control-Allow-Origin" or "blocked by CORS policy". On the developer's machine, it all worked.
Why it happens
- The browser is protecting your users. By default, a page can't read responses from another site. CORS is how a server opts in and names who may read its responses.
- Development hid it. Locally, the frontend and backend often share
localhost, or the development server quietly passes requests through. In production they'reapp.yoursite.comandapi.yoursite.com, or two different hosting platforms. - The allowed address doesn't match exactly.
https://yoursite.comandhttps://www.yoursite.comare different addresses. So arehttpandhttps. - Logins make it stricter. If requests carry cookies or login credentials, the server can't use the "allow everyone" wildcard. It must name the exact address.
- The check before the request fails. For many requests the browser first sends a "preflight" check. If the backend doesn't answer that check correctly, the real request is never sent.
- Sometimes it's a crash in disguise. If the backend errors before it adds its CORS headers, the browser reports a CORS error, even though the real problem is the crash. Check the status code in the Network tab.
How to fix it
- Read the exact console message. It says which address was blocked and which header was missing or wrong.
- Check the request's status in the Network tab. A 500 or 502 means fix the backend error first.
- On the backend, allow your production frontend's exact origin, for example
https://www.yoursite.com, with no trailing slash. Most frameworks have a CORS setting or package for this. Put the address in a setting, not in the code. - If logins or cookies are involved, allow credentials and name the origin explicitly. No wildcard.
- Make sure preflight
OPTIONSrequests get a successful reply with the same headers. - Don't "fix" it in the browser. Extensions that switch CORS off only work on your own computer, and public CORS proxies send your users' data through someone else's server.
When to call Preventionlabs
If one allowed address fixes it, you're done. Call us when the frontend and backend were built by different people, or deployed to places nobody can find, and getting them talking is one problem in a long list. A resurrection delivers the app running in your own hosting account with the user flows you list working end to end, including the frontend reaching its backend.
Submit your project for a free assessmentFree assessment. $10,000 AUD flat to get it live, only if we take it on and you go ahead.
Sources
- MDN Web Docs: Cross-Origin Resource Sharing (CORS)web 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 errorsweb reference
Most CORS errors can only be resolved on the server, because the server controls whether cross-origin access is allowed.
- MDN Web Docs: Cross-Origin Resource Sharing (CORS)web reference
CORS also relies on a mechanism by which browsers make a "preflight" request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request.
- MDN Web Docs: Access-Control-Allow-Originweb reference
Attempting to use the wildcard with credentials results in an error.