Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

CORS fails when you open an HTML file straight from disk

ErrorReason: CORS request not HTTP

Short 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.

  • Applies to Chrome and Firefox current, Python 3.12, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 cause
  2. 2Match what you see, then jump to its fix

    1. 1The address bar shows file:/// and the console says CORS request not HTTP.The page itself is loaded from file://Fix
    2. 2The 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. 3Your 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

What does the error look like?

  • Reason: CORS request not HTTP
  • Cross-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 3

2Fix

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.html

3Verify

The address bar shows http://localhost:8000 and the same fetch call now completes.

curl -I http://localhost:8000/index.html

Confirmed by developer.mozilla.org

Did this fix it?

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-origin

Confirmed by developer.mozilla.org

Did this fix it?

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-origin

2Fix

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 8000

3Verify

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.html

Confirmed by developer.mozilla.org

Did this fix it?

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:

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 fix

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.