A CORS error means the browser received a cross-origin response that did not satisfy the page's access rules; it does not necessarily mean that the API is unavailable. This guide gives you a reusable workflow for identifying whether the problem is a missing origin, a failed preflight request, unsupported credentials, a redirect, or an ordinary server-side failure.
Overview
Cross-Origin Resource Sharing, or CORS, controls how browser JavaScript can read responses from a different origin. An origin is the combination of scheme, host, and port. For example, https://app.example.com and https://api.example.com have different origins even though they share a parent domain.
The browser sends the request, evaluates the server's response headers, and exposes the response to your frontend only when the policy permits it. A request can therefore succeed at the network level while your React, JavaScript, or TypeScript code receives a CORS error. Tools such as cURL may show a perfectly normal status code because cURL does not enforce browser CORS rules.
Start by recording these values:
- The exact page origin, including scheme and port.
- The API URL and whether it redirects.
- The request method, such as
GET,POST,PUT, orDELETE. - Request headers added by your code, client library, or authentication layer.
- Whether cookies or other credentials are included.
Then inspect both the preflight, if one occurs, and the actual request in your browser's developer tools. For broader API diagnosis, pair this workflow with an API testing workflow for REST and GraphQL requests.
Request and response header reference
| Header | Direction | Purpose | What to check |
|---|---|---|---|
Origin | Request | Identifies the page making the cross-origin request. | Confirm it exactly matches the deployed frontend origin. |
Access-Control-Request-Method | Preflight request | States which method the browser intends to use. | Ensure the server allows that method. |
Access-Control-Request-Headers | Preflight request | Lists non-safelisted headers the actual request will send. | Ensure every required header is allowed. |
Access-Control-Allow-Origin | Response | Allows a specified origin to read the response. | Use the exact origin when credentials are involved; do not add a trailing slash. |
Access-Control-Allow-Methods | Preflight response | Lists methods permitted for the cross-origin request. | Include the intended method. |
Access-Control-Allow-Headers | Preflight response | Lists request headers permitted for the actual request. | Include headers such as Authorization or a custom header when needed. |
Access-Control-Allow-Credentials | Response | Allows the browser to expose responses to credentialed requests. | Set it to true only when the application requires credentials. |
Vary: Origin | Response | Helps caches distinguish responses generated for different origins. | Consider it when the server dynamically reflects an allowed origin. |
Checklist by scenario
Scenario 1: A simple request is blocked
- Open the failed request in the Network panel.
- Compare the request's
OriginwithAccess-Control-Allow-Originin the response. - Check for exact differences:
httpversushttps, a different port, a subdomain, or a trailing slash in the configured value. - Check the response on every relevant status path, including errors. A server may add CORS headers to successful responses but omit them from 401, 403, 404, or 500 responses.
Do not attempt to fix this by disabling browser security in development or by adding Access-Control-Allow-Origin to the frontend request. That is a response header and must be supplied by the server or an appropriate proxy.
Scenario 2: A CORS preflight request fails
A browser commonly sends an OPTIONS preflight before a request that uses a non-safelisted method, request header, or content type. In the Network panel, look for an OPTIONS request immediately before the failed request.
Use this decision tree:
- Is there an OPTIONS request? If no, inspect the actual response headers. If yes, continue.
- Did OPTIONS reach the intended server? A 404, 405, authentication redirect, proxy error, or timeout indicates infrastructure or routing work is needed.
- Does the response contain the correct allowed origin? If not, correct the server's origin configuration.
- Does it allow the requested method? Compare
Access-Control-Request-MethodwithAccess-Control-Allow-Methods. - Does it allow every requested header? Compare
Access-Control-Request-HeaderswithAccess-Control-Allow-Headers. - Does the actual request now fail? If preflight succeeds, inspect the actual response, application logs, authentication, and server status separately.
Some servers or middleware require an explicit route for OPTIONS; others handle it globally. Confirm that proxies, gateways, and authentication middleware do not reject preflight before CORS handling runs.
Scenario 3: Credentials or cookies are involved
For browser requests that must include cookies, your client may use credentials: "include":
fetch("https://api.example.com/profile", {
credentials: "include"
});The server must then return Access-Control-Allow-Credentials: true and an explicit allowed origin. A wildcard origin is not a substitute for a specific origin in a credentialed CORS exchange. Also verify cookie attributes, domain and path scope, secure transport requirements, and the site's broader cross-site cookie configuration. A CORS header can permit JavaScript to read a response, but it does not by itself make a cookie eligible to be sent.
Scenario 4: The request redirects
Inspect the request's redirect chain. An HTTP-to-HTTPS redirect, a missing trailing-slash route, a login redirect, or a regional hostname can change the request's destination and cause CORS or preflight failures. Test the final API URL directly, configure the client to use the canonical URL, and make sure each relevant response in the chain is compatible with your deployment. Avoid treating a redirect to an HTML login page as an API success.
Scenario 5: The API works in cURL but not in the browser
Simulate the browser's origin and preflight with cURL. cURL will return the server response but will not enforce whether a browser may expose it:
curl -i https://api.example.com/items \
-H 'Origin: https://app.example.com'For a preflight:
curl -i -X OPTIONS https://api.example.com/items \
-H 'Origin: https://app.example.com' \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: authorization, content-type'Compare the returned headers with the browser's request. If cURL shows a 500 or authentication error, fix the API first. If the API succeeds but lacks the required CORS headers, fix the server, gateway, or edge configuration. For payload inspection, a JSON formatter and validator can make malformed response data easier to separate from a CORS issue.
Framework configuration checkpoint
Configure CORS at the layer that owns the API response. In an Express-style application, the policy is commonly applied through middleware before routes are evaluated:
app.use(cors({
origin: "https://app.example.com",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true
}));The exact option names differ across frameworks and hosting platforms. In Next.js, serverless handlers, reverse proxies, API gateways, and edge functions may each have separate response configuration. Confirm where the response is actually generated rather than adding headers only to a frontend route.
What to double-check
- Origin matching: Treat the origin as an exact value. Development, preview, staging, and production deployments often use different schemes, hosts, or ports.
- Method and headers: A frontend library can add
Authorization, tracing, or custom headers even when your application code does not show them directly. - Content type: Changing a request to JSON can trigger a preflight where a simpler form request did not.
- Credentials: Align the browser option, server response headers, and cookie policy. Do not enable credentials globally without a specific need.
- Error responses: Add consistent CORS handling to the API's error path, while still returning accurate status codes.
- Caching: If allowed origins are selected dynamically, review cache behavior and use an appropriate
Varypolicy. - Deployment layers: Check the application, load balancer, CDN, API gateway, and platform redirects. The first layer that terminates or modifies the response may determine the final headers.
Keep the browser console message in context. “No Access-Control-Allow-Origin header” points toward response policy; “preflight response is not successful” points toward the OPTIONS exchange; “credential mode” messages point toward the relationship between credentials and the allowed origin.
Common mistakes
Adding CORS headers to the request. Browsers do not accept a client-side Access-Control-Allow-Origin header as permission. Configure the response.
Allowing every origin reflexively. A broad policy may hide configuration errors and is inappropriate for sensitive credentialed APIs. Define the smallest set of origins, methods, and headers that the application needs.
Testing only the happy path. Test authenticated requests, preflighted methods, validation errors, and server failures. Missing CORS headers on an error response can obscure the real problem.
Blaming CORS for an API failure. A 401, 403, 404, 500, DNS failure, or timeout still needs ordinary API debugging. Use server logs and an API client in addition to the browser.
Using a development proxy as the permanent fix. A local proxy can make two services appear same-origin during development, but production still needs a deliberate domain, gateway, or server policy.
Mixing CORS with CSRF protection. CORS controls whether browser JavaScript can read cross-origin responses. It is not a complete authorization or CSRF strategy.
When to revisit
Return to this checklist whenever a frontend connects to a new API, a deployment receives a new hostname, authentication changes, or a proxy, CDN, gateway, or serverless route is introduced. It is also worth reviewing before a release cycle that changes request methods, adds custom headers, moves from development to HTTPS, or begins sending cookies.
Make CORS part of an integration test rather than a last-minute browser check. For each supported frontend origin, exercise a normal request, a preflighted request, an authenticated request when applicable, and representative error responses. Record the expected methods, headers, credential behavior, and canonical API URL in the API documentation.
When a failure appears, use this short sequence:
- Capture the page origin and complete request URL.
- Inspect the Network panel for redirects,
OPTIONS, status codes, and response headers. - Repeat the request with cURL using the same origin and preflight headers.
- Fix the layer that owns the response: application, proxy, gateway, or hosting configuration.
- Retest success, preflight, credentials, and error paths.
That workflow remains useful as browser behavior, JavaScript clients, deployment platforms, and API boundaries change. For related debugging work, see the guide to browser storage and cookies when credentials are involved, and the OpenAPI tools comparison when you want to document and test the contract systematically.