Blocked by CORS policy
The browser hid a response from another site because that server never said your site is allowed to read it.
Also appears as
- Access to fetch at 'https://api.example.com/items' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
- Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/items. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.
Prompt to fix it
My app shows "blocked by CORS policy" in the browser console. Find the request that fails. If it calls our own server, add CORS headers there: allow the app’s origins (localhost for development and the published domain), allow the methods and headers the request uses, and answer OPTIONS preflight requests with 204 before any auth check. If it calls a third-party API, move that call into a server route so the browser only talks to our backend, and keep any API key on the server. Don’t use mode: "no-cors".
Paste into Layerwise or any AI coding tool.
What it means
Browsers only let a page read responses from its own origin: the scheme, domain, and port in the address bar. When your app at http://localhost:5173 calls https://api.example.com, the request still goes out, but the browser hides the response unless the server answers with an Access-Control-Allow-Origin header that names your site. CORS (cross-origin resource sharing) is the set of headers a server uses to grant that permission.
So the fix belongs on the server you’re calling. Changes to the fetch call can’t grant permission, and mode: 'no-cors' only returns an empty response your code can’t read.
Requests that send JSON, an Authorization header, or a method like PUT or DELETE trigger a preflight: the browser first sends an OPTIONS request to ask for permission. If the server doesn’t answer it with the right headers, you get the preflight version of this error even when the real endpoint works.
Common causes
- The app calls a third-party API straight from the browser, and that API only expects calls from servers.
- Your own backend has no CORS setup, or it allows
localhostbut not the published domain. - The server doesn’t handle the preflight
OPTIONSrequest, so requests with JSON bodies or auth headers fail. - The server crashed or redirected, and that error or redirect response carries no CORS headers.
How to fix it
- Open the Network tab, click the failed request, and note its URL and whether an
OPTIONSrequest ran before it. - If you own the server, return
Access-Control-Allow-Originwith your app’s exact origin, plusAccess-Control-Allow-MethodsandAccess-Control-Allow-Headersfor what the request sends. - Answer
OPTIONSrequests with status204and the same headers, before any sign-in check runs. - If you don’t own the API, call it from a route on your own backend and have the page call that route. This also keeps the API key out of the browser.
- Add the published domain to the allowed origins along with
localhostbefore you ship.
Example fix
const ALLOWED_ORIGINS = new Set([
'http://localhost:5173',
'https://my-app.com'
]);
export async function handle(request: Request) {
const origin = request.headers.get('Origin') ?? '';
const cors: Record<string, string> = ALLOWED_ORIGINS.has(origin)
? {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
Vary: 'Origin'
}
: {};
// Answer the preflight before routing or auth runs.
if (request.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: cors });
}
return Response.json({ items: [] }, { headers: cors });
}Related errors
All errorsFailed to fetch
The browser never got a usable response: the server was unreachable, or the browser blocked the request or its reply.
It looks like you're running in a browser-like environment
The AI SDK refused to run in the browser, because your secret API key would be visible to anyone who opens the page.
401 Unauthorized
The server couldn’t confirm who was calling: the request had no credentials, or its API key, session, or token was wrong or expired.
Unexpected token '<', "<!DOCTYPE "... is not valid JSON
Your code expected JSON, but the server sent back an HTML page, usually a 404 page or your app’s own index.html.
Build with an agent that checks its work
The Layerwise agent clicks through the live preview and reads the console, so it catches errors before you do.