Learning Tips | Published Nov 17, 2026 | 8 min read

CORS Explained: Browser Origins, Preflight, and Safe Configuration

Understand browser CORS, OPTIONS preflight, allowed origins and headers, credentials, common errors, and why CORS is not authentication.

Diagram of a browser CORS OPTIONS preflight, allowed origin and methods response, and the browser checking the actual API response

Cross-Origin Resource Sharing (CORS) controls when browser scripts may read responses from another origin. This guide explains origins, preflight requests, response headers, credential rules, common errors, and why CORS is not an authentication system.

A browser page can request resources from another origin, but browser scripts are not automatically allowed to read every cross-origin response. Cross-Origin Resource Sharing (CORS) is the HTTP-header-based protocol that lets a server state which browser origins may read a response. It is enforced by browsers; command-line clients and server-to-server requests do not use the browser's same-origin policy in the same way.

What counts as a different origin?

An origin is the combination of scheme, host, and port. A page at https://app.example.com and an API at https://api.example.com are different origins even though they share a parent domain. A change from HTTPS to HTTP, a different hostname, or a different port also creates a different origin. Paths do not define an origin.

Simple requests and preflight

Some cross-origin requests can be sent without a preliminary permission check. For requests that are more involved—for example, one that uses a non-safelisted method or request header—the browser first sends a preflight request using OPTIONS. The preflight includes the requesting origin and asks which method and headers are allowed. The server's response can include:

  • Access-Control-Allow-Origin to identify the allowed origin or, for suitable non-credentialed requests, the wildcard *.
  • Access-Control-Allow-Methods to list methods the browser may use.
  • Access-Control-Allow-Headers to list request headers the browser may send.
  • Access-Control-Max-Age to indicate how long a successful preflight result may be cached by the browser, subject to browser limits.

After a successful preflight, the browser sends the actual request. The actual response still needs the appropriate CORS headers: approving the preflight alone does not make a later response readable. For requests that do not need preflight, the server still has to return the matching CORS response header or the browser will withhold the response from the calling script.

Credentialed requests need an explicit origin

When a browser request includes credentials such as cookies or HTTP authentication, the client and server must opt into that behavior. The server uses Access-Control-Allow-Credentials: true and must return a specific allowed origin in Access-Control-Allow-Origin; it cannot use * for a credentialed CORS response. A frontend option such as credentials: 'include' does not grant itself permission.

If a server dynamically reflects an approved origin from the request's Origin header, it should validate that origin against an explicit allowlist and send Vary: Origin so caches distinguish the responses. Do not reflect arbitrary origins. CORS is not a substitute for authentication, authorization, CSRF defenses, or careful cookie settings.

A safe configuration workflow

  1. List the exact browser origins that should access the API, including scheme and any non-default port.
  2. Allow only the methods and request headers the feature requires.
  3. Decide whether cookies or other credentials are needed; if not, avoid enabling them.
  4. Return the policy headers on relevant responses, including errors, and handle preflight OPTIONS requests before normal endpoint logic rejects them.
  5. For dynamic origin selection, use an allowlist and emit Vary: Origin.
  6. Test from an actual browser at each intended origin, then inspect the network panel for the preflight and actual response.

A public read-only endpoint may allow any origin with Access-Control-Allow-Origin: * if exposing the response to all websites is intended and credentials are not involved. For private data, prefer an explicit allowlist and enforce the user's access rights on every request. The CORS header controls browser access to the response; it does not prevent a request from being sent by non-browser tools.

Diagnose common CORS errors

SymptomLikely causeWhat to inspect
Missing allow-origin headerThe server or an intermediary did not add CORS headers.Check the actual response, including error responses and redirects.
Preflight failsOPTIONS handling or allowed method/header values do not match the browser's request.Compare Access-Control-Request-Method and Access-Control-Request-Headers with the preflight response.
Wildcard with credentialsThe response uses * while the browser request includes credentials.Return a validated explicit origin and the credentials header, or stop sending credentials.
Works in curl but not in browsercurl does not enforce the browser's same-origin policy.Reproduce from the actual page origin and inspect browser devtools.

Keep the browser console's exact error and the network response together when debugging. The console message often points to a missing or mismatched header, while the network panel shows whether the failure occurred on OPTIONS or on the actual request. Avoid changing the policy to allow every origin as a quick fix unless public access is truly intended.

Related API design reading

CORS config is one part of reliable browser APIs. See our guides to HTTP method semantics and API rate limits and safe retries for related request and response behavior.

References

Want a structured Java learning path?

Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.

Explore the Java program   Ask for course guidance