Why you get a CORS error when the API is on your own domain
Reason: CORS header 'Access-Control-Allow-Origin' missingShort answer
The browser treats two URLs as one origin only when scheme, host and port all match. A page on https://example.com calling http://example.com, https://www.example.com or https://example.com:8443 is a cross-origin request. Compare the two origins exactly, then either make them identical or send a matching Access-Control-Allow-Origin.
Fix it fast
1Find out which cause you have
Print the page's origin in the browser console and compare it with the URL of the failing request. Look at the scheme, the host and the port, one by one.
// browser console on the page that fails console.log(location.origin); // then compare with the request URL shown in the Network tab2Match what you see, then jump to its fix
- 1
The page is on https and the request goes to http, or the API listens on another port such as :8000 or :8443The scheme or the port is differentFix - 2
The page is on example.com and the request goes to www.example.com, api.example.com or a static hostThe host is different (www, a subdomain or a CDN name)Fix
- 1
What does the error look like?
Reason: CORS header 'Access-Control-Allow-Origin' missing
Cause 1 of 2
The scheme or the port is different
1Check
Two URLs share an origin only when protocol, port and host are all the same. http defaults to port 80, so http://site.test and http://site.test:81 are different origins.
new URL(location.href).origin
new URL('http://example.com:8000/api').origin2Fix
Serve the page and the API on the same scheme and port, usually by proxying /api through the web server so the browser sees one origin. If they must differ, have the API send Access-Control-Allow-Origin with the exact page origin.
# response from the API for a page at https://example.com
Access-Control-Allow-Origin: https://example.com
Vary: Origin3Verify
Reload and check that the failing request no longer appears as cross-origin, or that the response now carries the allow-origin header.
curl -s -D - -o /dev/null -H "Origin: https://example.com" https://example.com/api/items | grep -i access-controlConfirmed by developer.mozilla.org
Cause 2 of 2
The host is different (www, a subdomain or a CDN name)
1Check
Different hosts are different origins even when they share a registered domain. The origin sent with the request is shown in the Origin request header in the Network tab.
curl -s -D - -o /dev/null -H "Origin: https://example.com" https://api.example.com/items | grep -i access-control2Fix
Either call the same host the page was loaded from, or make the server send the page's origin in Access-Control-Allow-Origin. A server that allows a list of origins should echo the matching one and add Vary: Origin.
Access-Control-Allow-Origin: https://example.com
Vary: Origin3Verify
Repeat the request with the Origin header and confirm the value matches the page origin.
curl -s -D - -o /dev/null -H "Origin: https://example.com" https://api.example.com/items | grep -i -E 'access-control|vary'Confirmed by developer.mozilla.org
How do I stop it coming back?
- Use relative URLs such as /api/items for your own backend so the origin cannot drift.
- Redirect http to https and www to the apex (or the reverse) before any page loads, so users only see one origin.
- Keep the API's allowed origins in config, one entry per real origin.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
The browser blocks your API response: no Access-Control-Allow-Origin header
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.
4 causes, each with a fixWeb apps
Apollo Client is blocked by CORS when the GraphQL server runs on AWS Lambda
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apollo Server does not set CORS for you when it runs behind a framework or integration, and on Lambda that layer is API Gateway.
5 causes, each with a fixWeb apps
A POST to a Google Apps Script web app returns 405 or a CORS error
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.
3 causes, each with a fixWeb apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fixWeb apps
A Chrome extension content script cannot read another site's response
Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy
Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help.
3 causes, each with a fixWeb apps
A web font served through CloudFront is blocked by CORS
Reason: CORS header 'Access-Control-Allow-Origin' missing
Browsers load web fonts with a CORS request, so the font response must carry Access-Control-Allow-Origin.
4 causes, each with a fix