Troubleshooting CORS Errors in Production: Step-by-Step Guide
Diagnose missing CORS headers, blocked OPTIONS preflights, proxy errors, and credential mismatches without weakening API security.
TechSilo
Technology writer & developer
A CORS error often appears in the browser console as:
Access to fetch at 'https://api.example.com/data'
from origin 'https://app.example.com' has been blocked by CORS policyThis message means that the browser did not receive permissions needed to expose the response to JavaScript. It does not always mean the backend request never reached your server.
Step 1: Reproduce and inspect the network request
Open browser DevTools → Network, enable Preserve log, and retry. Inspect the request URL, method, Origin header, and response status. Look for an OPTIONS request immediately before the intended request.
Also check for redirects: an authentication gateway or hosting platform may redirect a preflight to a login page, which can break the CORS flow.
Step 2: Check preflight behavior
A request with JSON content or an Authorization header often needs a preflight.
curl -i -X OPTIONS "https://api.example.com/data" \
-H "Origin: https://app.example.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type,authorization"The API should return an appropriate successful preflight response with the allowed origin, method, and request headers. If the result is 404, 405, 500, or a redirect, fix the route, reverse proxy, or middleware ordering.
Step 3: Inspect the actual response
curl -i "https://api.example.com/data" \
-H "Origin: https://app.example.com"The response should include the matching Access-Control-Allow-Origin value. If credentials are involved, it must also provide the appropriate credential permission. A wildcard origin cannot be used for browser requests that include credentials.
Step 4: Check the origin exactly
The following are not interchangeable:
https://example.com
https://www.example.com
http://example.com
https://example.com:3000Whitelist the precise scheme, host, and port of your production frontend. Avoid an unrestricted wildcard merely to make the warning disappear.
Step 5: Look beyond your application code
A route can behave differently behind a CDN, reverse proxy, firewall, or hosting platform.
- Does the proxy forward OPTIONS requests?
- Are CORS headers present on error responses, including 401 and 500?
- Does the CDN cache a response intended for another origin?
- Does a redirect change the scheme or hostname?
- Did an environment variable retain a staging-domain allowlist?
If responses vary according to the requesting origin, make sure caches handle that variation correctly—for example, by respecting Vary: Origin.
Step 6: Distinguish CORS from authentication
An expired session may return 401 or redirect to login; the browser might show only a CORS error if that error response omits CORS headers. First diagnose the underlying authentication failure.
CORS is not API protection: malicious clients outside browsers can make HTTP requests regardless of these headers.
Don't use these shortcuts
Disabling browser security, using a public CORS proxy, and reflecting every origin are not production fixes. Address the server or gateway policy instead.
Final verification
Test from your deployed frontend with an authorized origin and from an intentionally unauthorized origin. Confirm the first succeeds and the second cannot read protected responses, while the API still authenticates and authorizes every sensitive request.
For the configuration side, read the [Practical CORS Security Guide](/blog/practical-cors-configuration-security-guide-msfk7phr).
Want to put this into practice?
Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.
Related blog posts
Console.log Alternatives for Efficient JavaScript Debugging
Use browser breakpoints, console.table, console.trace, timing APIs, and structured logs to debug without flooding production consoles.
Read postPractical CORS Configuration Security Guide for APIs
Secure cross-origin browser requests with explicit origin allowlists, correct preflight handling, and credential rules.
Read postAPI Authentication Patterns: Sessions, Tokens, and Service Credentials
Compare session cookies, bearer tokens, OAuth, and API keys, with practical protections against token leakage and authorization mistakes.
Read postFix Next.js Hydration Mismatches: Causes and Safe Solutions
Troubleshoot React hydration errors in Next.js from dates, browser-only APIs, random output, incorrect markup, and third-party libraries.
Read postFix TypeScript 'any' Spreading: Validate Unknown Values Safely
Troubleshoot TypeScript spread errors with unknown values, object narrowing, safe merge patterns, and typed generics.
Read post