← All guides

Web & APIs · 8 min read

CORS Explained

Few browser errors cause as much confusion as "blocked by CORS policy". The request works in curl, works in Postman, works from the backend — and fails in the browser. The reason is that CORS is not a server security feature that blocks requests; it is a browser rule about which pages may read which responses. Once that is clear, the fixes become straightforward.

This guide covers the same-origin policy CORS relaxes, simple and preflighted requests, the response headers that matter, credentials, the common errors, and what CORS does and does not protect against.

Try it yourself with the related tool.

Browse HTTP status codes →

Advertisement

The same-origin policy

An origin is the combination of scheme, host and port: https://app.example.com:443. By default, JavaScript on one origin can send requests to another origin but cannot read the response. Without this rule, any website you visit could quietly call your bank's API with your cookies and read your balance. https://example.com and https://api.example.com are different origins, and so are http://localhost:3000 and http://localhost:8080.

CORS — Cross-Origin Resource Sharing — is how a server opts in to letting specific other origins read its responses. The server says "pages from this origin may read me", and the browser enforces it.

Simple requests

A GET, HEAD or POST with only basic headers and a content type of text/plain, multipart/form-data or application/x-www-form-urlencoded is a "simple" request. The browser sends it directly with an Origin header. The server replies as usual; if the response includes Access-Control-Allow-Origin matching the page's origin (or *), the browser hands the response to the script. If not, the request still reached the server and still ran — the browser just hides the result.

Preflight requests

Anything else — a PUT or DELETE, a JSON body with Content-Type: application/json, or a custom header such as Authorization — triggers a preflight. Before the real request, the browser sends an OPTIONS request listing the method and headers it intends to use in Access-Control-Request-Method and Access-Control-Request-Headers. The server must answer with a 2xx status and matching Access-Control-Allow-Methods and Access-Control-Allow-Headers. Only then does the real request go out.

A frequent bug is a server or framework that requires authentication on every route, including OPTIONS. The preflight carries no credentials, gets a 401, and the browser reports a CORS failure. Let OPTIONS through before the auth check. Access-Control-Max-Age lets the browser cache a preflight result so it is not repeated on every call.

Credentials and cookies

By default, cross-origin fetch does not send cookies. With credentials: "include", it does — but then the server must reply with Access-Control-Allow-Credentials: true and an exact origin in Access-Control-Allow-Origin. The wildcard * is not allowed with credentials. Servers that support several front-ends usually check the incoming Origin against an allow-list and echo it back, adding Vary: Origin so caches do not serve one origin's headers to another.

Reading the error messages

"No Access-Control-Allow-Origin header is present" means the response lacked the header entirely — often because the request errored (a 500 or 404 page rarely includes CORS headers) or the CORS middleware is not running on that route. "Response to preflight request does not pass access control check" points at the OPTIONS response. "The value of the header must not be the wildcard when credentials mode is include" means exactly that. In every case, open the network tab and look at the actual response headers rather than guessing.

Fixing it properly

CORS is configured on the server that owns the resource, never in the front-end code. Adding a mode: "no-cors" option does not fix anything; it produces an opaque response your code cannot read. If you do not control the API, call it from your own backend or a serverless function instead of from the browser. In development, a dev-server proxy makes the API appear same-origin and avoids CORS entirely.

What CORS does not do

CORS protects users' data in browsers. It does not stop anyone from calling your API with curl, a script or a server, and it does not prevent requests from being sent — simple requests reach the server whether or not the response is readable. Authentication, authorization and CSRF protection are still required. Treat Access-Control-Allow-Origin: * as fine for genuinely public, unauthenticated data, and as a red flag anywhere a user's session is involved.

Quick reference

Related guides