CORS error blocked by policy
Locate the failed assumption, reproduce it, then repair the cause.
What actually caused it
The browser is enforcing cross-origin access rules. Your page requested a resource at a different origin, and the response or required preflight did not grant the permission needed for JavaScript to read it. The exact console message includes the origins and the failed policy condition.
Origin includes scheme, host, and port. A request that works in a server script or command-line client may still fail in the browser because those clients do not enforce browser . Fetch often exposes the failure to your code as a generic TypeError, while the browser console and Network panel contain the policy details.
Some cross-origin requests are sent before their responses are blocked from being read. Others require an OPTIONS preflight that must succeed first. is a server permission mechanism, not an authentication replacement.
Minimal reproduction
A browser request without API permission
fetch("https://api.example.com/items")
.then(response => response.json())
.then(items => console.log(items))
.catch(error => console.error(error.message));This is a request shape, not a live demonstration endpoint. The error depends on the ’s actual response headers.
The fix
If you control the , configure it to allow the specific frontend origin and the and request headers actually needed. Handle preflight OPTIONS requests. Credentialed requests need explicit origin permission and Access-Control-Allow-Credentials; a wildcard origin is not valid with credentials.
If you do not control the , use a supported same-origin backend integration that is authorized to call it. Do not create an unrestricted public proxy. The proxy should allow only intended destinations and preserve authentication and validation boundaries.
Do not set mode: "no-" expecting readable : it yields an opaque response. Adding Access-Control-Allow-Origin as a request header also cannot grant permission; it is a response header controlled by the server.
Allow the intended origin on your Node API
import { createServer } from "node:http";
createServer((request, response) => {
const allowedOrigin = "https://learn.example.com";
if (request.headers.origin === allowedOrigin) {
response.setHeader("Access-Control-Allow-Origin", allowedOrigin);
}
response.setHeader("Vary", "Origin");
response.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
if (request.method === "OPTIONS") {
response.writeHead(204).end();
return;
}
if (request.method !== "GET") { response.writeHead(405).end(); return; }
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify({ items: [] }));
}).listen(3000);Run this as an ESM Node server, not in the browser sandbox. Replace allowedOrigin with your deployed frontend origin. This minimal public GET endpoint uses no credentials or custom request headers.