How to Fix
"Cross origin requests are only supported for protocol schemes: http" When Opening Local HTML?To fix "Cross origin requests are only supported for protocol schemes: http" (or chrome-extension, data, etc.) when opening a local HTML file, serve the project over a local HTTP server (such as VS Code Live Server, npx serve, or python3 -m http.server) instead of opening index.html directly via file://. Browsers treat file:// URLs as opaque/restricted origins for fetch() and XMLHttpRequest, so serving files over http://localhost:... gives them a real HTTP origin.
1. Serving Local HTML Over http://localhost with a Development Server
Run a lightweight local static server in your project folder and open the printed http://localhost:... URL in your browser rather than double-clicking the .html file on disk:
# Start a local static HTTP server from the project directory (Node.js or Python)
# Option A (Node.js):
# npx serve . -p 3000
# Option B (Python 3):
# python3 -m http.server 3000
// Browser-side helper that verifies protocol scheme before calling fetch()
async function fetchLocalResource(url) {
if (typeof url !== "string" || url.trim() === "") {
throw new TypeError("Expected a valid resource URL.");
}
if (window.location.protocol === "file:") {
throw new Error(
'Cannot fetch over file://. Serve this HTML file via a local HTTP server (e.g., http://localhost:3000).'
);
}
const response = await fetch(url, { cache: "no-store" });
if (!response.ok) {
throw new Error(`HTTP ${response.status} while loading "${url}"`);
}
return response.text();
}
- Real HTTP origin:
http://localhost:3000satisfies browser origin and CORS rules for local assets, JSON files, and ES modules. - Early protocol guard: Detecting
window.location.protocol === "file:"surfaces a clear message immediately during local development.
2. Embedding Static Data or Bundling When No Server Is Available
If a standalone single-file HTML page must run offline without a local HTTP server, inline the JSON/data directly inside a <script type="application/json" id="..."> tag or bundle assets with a build tool instead of fetching external local files over file://:
// Read JSON embedded safely inside a <script type="application/json"> tag without fetch()
function readEmbeddedJson(scriptId, fallback = null) {
if (typeof scriptId !== "string" || scriptId.trim() === "") {
return fallback;
}
const scriptEl = document.getElementById(scriptId);
if (!(scriptEl instanceof HTMLScriptElement)) {
return fallback;
}
const rawText = (scriptEl.textContent || "").trim();
if (!rawText) {
return fallback;
}
return JSON.parse(rawText);
}
// Example usage in HTML + JS:
// <script type="application/json" id="initial-data">{"items":["a","b"]}</script>
// const data = readEmbeddedJson("initial-data", { items: [] });
- Works over
file://: ReadingtextContentfrom an inline<script type="application/json">element does not make a network orfile://fetch request. - Non-mutating parse: Returns a freshly parsed object/array each time without altering the DOM node’s text.
3. Common Traps to Avoid
Resolving "Cross origin requests are only supported for protocol schemes: http" when opening local HTML often runs into a few pitfalls:
- Opening files via
file:///C:/...orfile:///Users/...: Even relative paths like./data.jsonfail onfile://origins in Chromium-based browsers when requested viafetch()orXMLHttpRequest. - Weakening browser security flags globally: Launching browsers with
--allow-file-access-from-filesis a risky workaround for local testing—use a local HTTP server likehttp://localhostinstead. - Confusing
file://errors with server CORS headers:Access-Control-Allow-Originheaders only apply to HTTP/HTTPS responses; switching fromfile://tohttp://localhostsolves the scheme restriction itself.
TL;DR: Serve your local HTML through a local HTTP server (npx serve, python3 -m http.server, or Live Server) and open http://localhost:... instead of file://, or embed data inside <script type="application/json"> when running offline without a server.
