How do CORS preflight and response headers work?
Useful first: Same-origin policy · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALExplain why a CORS error does not tell you that a request never arrived.
Separate sending from reading
The server supplies permission to share
A page at https://reader.example.com wants data from https://api.example.com. These are different origins. Building on the same-origin policy, CORS lets the API describe which browser origins may read its response. The browser enforces that permission.
A response can arrive but remain unreadable
Some requests can be sent without a CORS preflight. If the returned response lacks the required sharing permission, browser code cannot read it, even if the server processed the request. A CORS error is not a receipt proving that nothing happened.
[2] [3]Choose the right kind of fix
The API must supply appropriate response headers. Adding an allow-origin header to your outgoing request does not grant permission. Switching to no-cors is not a way to read blocked JSON: a cross-origin response in that mode is opaque to your script. Server authentication and protection against unwanted actions remain separate responsibilities.
Check delivery and response access separately; a browser error is not proof of rollback.
A browser CORS error proves the server did not receive or process the request.
A request can reach the server before the browser refuses to share its response.
Show the explanation
The response was not shared with the requesting script. CORS controls sharing with browser code. Missing response permission does not undo work the server already performed.
YOUR GOALMatch a custom request header to the permission needed before the real request.
Follow one complete preflight exchange
A custom header changes this GET
Consider this illustrative call from https://reader.example.com. The example domains are not a running demo:
const response = await fetch("https://api.example.com/reading", {
method: "GET",
credentials: "omit",
headers: { "X-Demo-Trace": "lesson-1" }
});X-Demo-Trace is not a CORS-safelisted request header. Assuming no matching cached preflight permission, the browser first asks whether it may send that header. GET alone does not guarantee a preflight-free request.
Read the request and allow headers
The relevant preflight request fields are:
OPTIONS /reading HTTP/1.1
Host: api.example.com
Origin: https://reader.example.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: x-demo-traceOne valid preflight reply is:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://reader.example.com
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: X-Demo-TraceThe method field describes the intended GET; the request-header field lists a header name, not its value. The allow-header field authorizes that name. A plain HTTP Allow header does not replace these CORS fields.
Check the actual response too
After successful preflight, the browser can send the GET with X-Demo-Trace: lesson-1. The GET response still needs an appropriate Access-Control-Allow-Origin value. A successful OPTIONS reply does not grant access to every later response. Inspect the preflight and the actual exchange separately; HTTP 200 alone is not a complete CORS diagnosis.
Follow OPTIONS permission, the actual request, and final response sharing as distinct steps.
Only POST or other writing methods can need preflight.
A GET with a non-safelisted custom request header can need preflight too.
Show the explanation
The preflight fails because X-Demo-Trace was not permitted, so this GET is not sent. The custom request header needs permission. Allowing the origin and method does not approve that header.
YOUR GOALDiagnose three independent decisions after a server adopts CORS.
Separate credentials, cache variants and header visibility
Credential mode changes origin matching
With credentials: "include", response sharing requires the specific requesting origin and Access-Control-Allow-Credentials: true; Access-Control-Allow-Origin: * does not work, even if no cookie happens to be available. A required preflight must permit this credentialed use too. Cookie eligibility still follows cookie rules and browser policy; the option cannot force a blocked cookie to be sent.
Vary a response that depends on Origin
If an API supports several approved origins, check the incoming origin against that approved set and return one matching value, rather than reflecting arbitrary input or sending a comma-separated list. When the response varies by Origin, send Vary: Origin so caches distinguish those variants. This is a cache-selection rule, not a substitute for deciding whether sensitive content may be stored.
Expose headers in the response direction
Access-Control-Allow-Headers concerns headers the script wants to send. To let code read a non-safelisted response header such as X-Reading-Version, the actual response can include Access-Control-Expose-Headers: X-Reading-Version. That does not authorize a request header with the same name. With credential mode include, an expose-headers value of * is a literal header name rather than a wildcard.
Check origin and credentials, cache variation, and response-header exposure independently.
Allow-Headers and Expose-Headers give the same permission in either direction.
Allow-Headers governs request headers; Expose-Headers makes additional response headers readable.
Show the explanation
Access-Control-Expose-Headers: X-Reading-Version on the actual response. Expose-Headers makes this non-safelisted response header available to browser code. The other names concern the proposed request, not reading response headers.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- WHATWG — Fetch Standard: CORS protocol, preflight and HTTP caches
- MDN — Cross-Origin Resource Sharing (CORS)
- MDN — Using the Fetch API
- MDN — Access-Control-Expose-Headers
- MDN — Access-Control-Allow-Origin