Fix CORS errors by configuring the API or gateway to return the headers required by your browser’s request. The usual causes are a mismatched origin, a failed OPTIONS preflight, or incompatible credential settings.
The fix belongs in the server configuration, not a browser security workaround. Start by identifying which request failed and what the browser expected.
Key takeaways
- CORS controls whether browser JavaScript can access cross-origin responses; it doesn’t authenticate users or authorize API operations.
- Compare the request’s origin, method, and headers with the server’s CORS response.
- Credentialed requests require an explicit allowed origin, while preflight requests need successful handling before the actual request can proceed.
What CORS errors mean
Cross-Origin Resource Sharing lets servers declare which origins may read their responses through browser JavaScript. An origin consists of a scheme, hostname, and port.
For example, https://app.example and https://api.example are different origins. Likewise, http://localhost:3000 differs from http://localhost:5173. URL paths don’t form part of an origin.

As MDN’s CORS guide explains, browsers evaluate response headers before exposing cross-origin responses to calling JavaScript. Some requests first require an OPTIONS permission check called a preflight.
CORS isn’t API authentication or authorization. Your API must still validate credentials and enforce tenant permissions, regardless of the caller’s origin. Tools outside the browser don’t enforce its CORS restrictions.
A CORS failure can hide a response after the server has already processed the request. Don’t blindly retry writes because JavaScript couldn’t read the result.
Identify the failing request in DevTools
Open Chrome DevTools, select Network, enable Preserve log, and reproduce the failure. Keep the Console visible because its CORS message often names the failed rule.

Inspect the origin and response headers
Select the API request and open Headers. Under Request Headers, find Origin. Compare that exact value with Access-Control-Allow-Origin under Response Headers.
A missing header, different scheme, or wrong port explains many failures. Also inspect the status and final URL. A request may have hit a login page, redirect, or gateway error instead of the intended endpoint.
Record the exact Console message. “No ‘Access-Control-Allow-Origin’ header” and a credentials-related wildcard error require different fixes.
Find the OPTIONS request
Look for an OPTIONS request to the same endpoint. If it fails, the browser normally won’t send the intended API request.
Inspect Access-Control-Request-Method and Access-Control-Request-Headers. These describe the method and headers the browser wants permission to send.
A missing OPTIONS entry isn’t automatically a problem. The request may not require preflight, or the browser may have cached permission. If neither request has an HTTP response, investigate DNS, TLS, connectivity, or mixed-content blocking before changing CORS settings.
Follow this diagnostic checklist
Use this sequence to avoid changing several settings without knowing which one failed.
- Reproduce the failure in the affected browser and environment. Record the page origin, endpoint, method, and Console message.
- Locate the actual request and any matching
OPTIONSrequest. Determine whether preflight failed or the actual response failed the CORS check. - Compare the request’s
Originwith the returnedAccess-Control-Allow-Origin. Match the scheme, hostname, and port. - For preflight, compare the requested method and headers with
Access-Control-Allow-MethodsandAccess-Control-Allow-Headers. - If cookies are involved, inspect the frontend’s credential option and the server’s origin and credential headers together.
- Apply the fix at the layer producing the response, then repeat the browser request. Exercise successful responses, authentication failures, and server errors.
Capture request identifiers and relevant configuration during diagnosis. Redact cookies, tokens, and authorization values before sharing logs or screenshots.
Fix missing or mismatched allowed origins
Return one approved origin
Configure an explicit allowlist for production, staging, and local development. Compare the incoming Origin against that list before returning it.
For an approved request, return one Access-Control-Allow-Origin value. Don’t return a comma-separated list of origins, and don’t include a URL path or trailing slash.
Avoid reflecting every supplied origin without validation. That turns a restricted policy into an unrestricted one. For intentionally public, non-credentialed resources, * can be appropriate.
MDN’s explanation of the missing allowed-origin header describes why browsers block response access when that permission is absent.
Cover error responses and caching
Place CORS handling early enough that approved origins receive the necessary headers on error responses too. Otherwise, an expired token might produce a 401 response that the frontend only sees as a CORS failure.
Once the response becomes readable, diagnose the underlying status separately. A 401 usually concerns missing or rejected credentials; a 403 means the server refuses the operation.
When the allowed-origin value changes according to the request’s Origin, return Vary: Origin. This tells caches that responses can differ by origin.
Make preflight and request permissions agree
Handle OPTIONS before application authentication
Browsers commonly preflight requests that use PUT, an Authorization header, or Content-Type: application/json.
Route OPTIONS to a CORS handler before middleware that requires the actual request’s credentials. Preflight doesn’t carry those credentials, so requiring a session cookie or bearer token can produce an immediate 401 or 403.
Validate the requested origin and permissions, then return a successful response, commonly 204 No Content. Keep authentication and authorization enforced on the actual API operation.
Also prevent redirects on this path. A redirect to a login screen or another hostname can break preflight handling.
Allow the requested method and headers
For a JSON PUT request with bearer authentication, the exchange can include these headers.
| Direction | Header | Example value |
|---|---|---|
| OPTIONS request | Origin | https://app.example |
| OPTIONS request | Access-Control-Request-Method | PUT |
| OPTIONS request | Access-Control-Request-Headers | authorization, content-type |
| 204 response | Access-Control-Allow-Origin | https://app.example |
| 204 response | Access-Control-Allow-Methods | PUT |
| 204 response | Access-Control-Allow-Headers | Authorization, Content-Type |
The response permits the intended operation; merely allowing OPTIONS isn’t enough.
List the headers your application sends, including custom API-key or tracing headers. The subsequent API response must also include Access-Control-Allow-Origin. A successful preflight doesn’t replace that requirement.
Configure credentialed requests safely
Pair cookies with an explicit origin
For cross-origin cookie authentication, frontend Fetch requests generally need credentials: "include". The server must return both an explicit approved origin and Access-Control-Allow-Credentials: true.
Return those permissions on the preflight response when applicable and on the actual response. Explicitly list permitted methods and headers as well.
A wildcard origin doesn’t work with credentialed requests. MDN documents this wildcard and credentials mismatch, a common cause of failures after enabling cookie-based sessions.
Separate cookie rules from bearer authentication
Correct CORS headers don’t override cookie restrictions. Cross-site cookies generally require SameSite=None; Secure, and browser third-party-cookie policies can still prevent their use. Cross-origin and cross-site aren’t identical concepts.
Keep CSRF protection for cookie-authenticated writes. CORS alone doesn’t stop every cross-site request from reaching your server.
An explicitly supplied bearer Authorization header requires preflight permission, but it doesn’t by itself require credentials: "include". Add that option when the application needs browser-managed credentials, rather than treating it as a universal CORS fix.
Fix the response-producing layer
Your application may have correct CORS middleware while a CDN, reverse proxy, or API gateway returns the failing response.
Identify the component responsible for the observed status. An upstream timeout, rate-limit response, or gateway-generated authentication error may bypass application middleware entirely. Configure consistent CORS handling there for approved origins.
Also give one layer clear ownership of the policy. If both the application and proxy append Access-Control-Allow-Origin, the browser can reject the duplicate header values.
After deployment, test through the public endpoint rather than only calling the application server directly. Include an approved origin and a disallowed origin in regression tests. The disallowed origin shouldn’t receive permission to read protected responses.
MDN’s practical CORS configuration guidance supports keeping permissions limited to the cross-origin access the resource actually needs.
Handle third-party APIs through the right architecture
If another company owns the API, frontend code can’t force it to return CORS permission. Adding Access-Control-Allow-Origin to request headers doesn’t grant access; that header belongs in the server’s response.
For APIs intended for server-side use, call them through your backend or a backend for frontend (BFF). The browser talks to your application, while your server holds credentials and calls the provider.
Keep that backend restricted to approved operations and destinations. An unrestricted forwarding endpoint can become an open proxy.
This distinction also matters for OAuth. A browser application can’t protect a bundled client secret, and direct browser token exchange requires a CORS-capable token endpoint. A BFF keeps tokens server-side, but still needs secure sessions, CSRF protection, and tenant authorization.
Frequently asked questions
Why does Postman work when the browser fails?
Postman and server-side HTTP clients don’t enforce browser CORS rules. Their success shows that a request can reach the API and receive a response, but it doesn’t prove browser JavaScript has permission to read it.
Compare the method, headers, and credentials too. The two requests may differ.
Does Fetch’s no-cors mode solve the problem?
No. mode: "no-cors" restricts the request and returns an opaque response that JavaScript can’t inspect. It doesn’t provide readable API data.
Disabling browser security, installing bypass extensions, or using an untrusted public proxy also leaves the production configuration unfixed. Correct the server policy or use an appropriately secured backend.
Fix the failed rule, then test the full path
Reliable CORS troubleshooting starts with the browser’s actual request and the server’s actual response. Match the origin, resolve preflight failures, and configure credentials consistently.
Keep authorization separate and test through the deployed gateway, including error responses. The fix is complete when legitimate browser requests work without weakening the API’s security controls.