CORS fails when you open an HTML file straight from disk
Reason: CORS request not HTTPShort answer
A page opened from disk has an opaque origin, so browsers treat every request it makes as cross-origin, and CORS only works over http or https. The fix is to serve the folder from a local web server and open http://localhost. Adding headers to the API cannot fix a file:// page.
Fix it fast
1Find out which cause you have
Look at the address bar. If it starts with file:///, that is the cause. A page on a real web server shows http:// or https://.
console.log(location.protocol, location.origin) // in the page's DevTools console: 'file:' and 'null' mean this cause2Match what you see, then jump to its fix
- 1
The address bar shows file:/// and the console says CORS request not HTTP.The page itself is loaded from file://Fix - 2
The page URL is http://localhost:8000 and the console now reports a missing Access-Control-Allow-Origin header.The page is served locally but calls an API on another originFix - 3
Your own allowlist check fails and logs an origin of null, or the response lacks Access-Control-Allow-Origin for a sandboxed iframe or a redirect.The server rejects Origin: nullFix
- 1
What does the error look like?
Reason: CORS request not HTTPCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///Origin: null
Cause 1 of 3
The page itself is loaded from file://
1Check
Browsers give local files an opaque origin. Requests from such a page send Origin: null, and a request to another local file counts as cross-origin. CORS applies only to the http and https schemes.
python3 -m http.server --help | head -n 32Fix
Serve the project folder with any local web server and open the page through http://localhost.
cd my-project
python3 -m http.server 8000
# open http://localhost:8000/index.html3Verify
The address bar shows http://localhost:8000 and the same fetch call now completes.
curl -I http://localhost:8000/index.htmlConfirmed by developer.mozilla.org
Cause 2 of 3
The page is served locally but calls an API on another origin
1Check
Moving to a server fixed the file:// problem, but http://localhost:8000 and your API are different origins. This is now an ordinary CORS case and the API must allow the page's origin.
curl -s -o /dev/null -D - https://api.example.com/items \
-H "Origin: http://localhost:8000" | grep -i "^access-control"2Fix
Add http://localhost:8000 to the API's allowed origins for development, as described in the main CORS page.
// Express, development only
app.use(cors({ origin: ['http://localhost:8000'] }))3Verify
The curl command above should print Access-Control-Allow-Origin: http://localhost:8000.
curl -s -o /dev/null -D - https://api.example.com/items -H "Origin: http://localhost:8000" | grep -i allow-originConfirmed by developer.mozilla.org
Cause 3 of 3
The server rejects Origin: null
1Check
The Origin header is null for non-http schemes such as file, data and blob, for iframes sandboxed without allow-same-origin, and after cross-origin redirects. It is not a value you control from the frontend.
curl -s -o /dev/null -D - https://api.example.com/items -H "Origin: null" | grep -i allow-origin2Fix
Do not add null to the allowlist to make a file:// page work. Move the page to a real origin instead, using the local server above.
python3 -m http.server 80003Verify
Reload through http://localhost:8000 and check the Origin header in DevTools: it shows the localhost origin, not null.
curl -I http://localhost:8000/index.htmlConfirmed by developer.mozilla.org
How do I stop it coming back?
- Start every static prototype with a dev server (Vite, python -m http.server) rather than double-clicking the file.
- Keep relative asset paths so the same files work on localhost and in production.
- List only real origins in the API allowlist.
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:
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
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
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
The cors-anywhere.herokuapp.com demo proxy stopped working
Missing required request header. Must specify one of: origin,x-requested-with
The public cors-anywhere.herokuapp.com server is only a demo, is rate limited, and since February 2021 requires an opt-in.
2 causes, each with a fixWeb apps
The browser rejects the CORS preflight because the OPTIONS reply is not a 2xx
The request in the Network tab is an OPTIONS call with a red status, and the real GET or POST never appears
Before some cross-origin calls the browser sends an OPTIONS preflight and treats any answer outside 200 to 299 as a network error.
3 causes, each with a fixWeb apps
A public website cannot reach your local dev server: local network request blocked
Look for and connect to any device on your local network
Chrome treats a request from a public website to localhost or a private IP as a local network request.
3 causes, each with a fixWeb apps
django-cors-headers settings have no effect
CORS_ALLOWED_ORIGINS not working
django-cors-headers only works when corsheaders is in INSTALLED_APPS, CorsMiddleware sits high in MIDDLEWARE (before CommonMiddleware), and at least one origin setting is defined.
4 causes, each with a fix