CORS Errors Explained: Preflight, Credentials and Fixes
See why browsers block cross-origin requests, when a preflight is sent, how credentials change the rules, and how to configure CORS on common servers.

What CORS actually protects
Browsers follow the same-origin policy: a page from one origin, meaning scheme, host and port together, may send requests elsewhere but may not read the responses. Without that rule, any website you visit could read your webmail or your bank's API using the cookies already in your browser. Cross-Origin Resource Sharing is the controlled exception. The server answers with headers that say which other origins may read its responses and under what conditions, and the browser enforces them.
Two points follow from this. First, CORS is enforced only in browsers, which is why the same request works in curl or Postman. Second, CORS does not secure your API against other servers or scripts; it only governs what web pages may read. Authentication and authorisation still belong on the server.
Simple requests and preflights
Some requests are considered simple: GET, HEAD or POST, with only a handful of safelisted headers, and a Content-Type of form-urlencoded, multipart form data or text/plain. The browser sends these straight away and checks the response headers afterwards. Anything else triggers a preflight. Before the real request, the browser sends an OPTIONS request with the origin, the intended method and the list of non-safelisted headers, and waits for the server to approve them. A JSON body or an Authorization header is enough to need one.
The preflight answer must have a success status, must not redirect, and must contain Access-Control-Allow-Origin matching the page, Access-Control-Allow-Methods covering the method, and Access-Control-Allow-Headers covering every non-safelisted header. Access-Control-Max-Age lets the browser cache the approval; Chrome caps it at two hours.
Credentials change the rules
When fetch uses credentials: 'include', or axios uses withCredentials, the browser sends cookies and expects stricter answers. The allowed origin must be the exact origin, never the wildcard. Access-Control-Allow-Credentials must be true. Wildcards in the methods and headers lists are read literally. Servers that support several front ends therefore check the incoming Origin against an allowlist and echo it back, and they must add Vary: Origin so caches keep the answers apart.
Reading response headers
Even after a successful request, JavaScript can read only seven response headers by default, such as Content-Type and Cache-Control. A custom header like X-Request-Id or a rate-limit counter is visible in DevTools but undefined in your code until the server lists it in Access-Control-Expose-Headers.
Debugging with a simulator
The CORS Header Builder and Preflight Explainer lets you describe both sides. Enter your server policy on the left and the request your front end makes on the right. The simulator shows whether a preflight happens, the request and response headers involved, and each browser check in order with the same wording you see in the console. When a check fails, the fix button adds what is missing, and the config tab writes the corrected policy for Express, Nginx, Apache, hosting platforms or serverless functions.
Real deployments add their own twists, so after fixing the policy, test in the browser. Open the network panel, find the OPTIONS row and compare its response headers with what the simulator expected. If they differ, a proxy, CDN or framework is probably adding, removing or duplicating headers. Make sure error responses also carry CORS headers, otherwise a 401 or 500 shows up as a confusing CORS failure instead of the real error.
A safe default policy
List exact origins instead of using a wildcard, allow only the methods and headers your front end uses, enable credentials only when cookies are truly needed, send Vary: Origin, and keep a moderate max-age. Never echo back any origin that asks, which effectively disables the protection, and avoid allowing the null origin.



