CORS Error Troubleshooting Guide: Diagnose Preflight Requests, Headers, and Local Development Failures
corsweb-apisjavascriptapi-debuggingfrontend-developmentdeveloper-tools

CORS Error Troubleshooting Guide: Diagnose Preflight Requests, Headers, and Local Development Failures

FFlorence Cloud Editorial Team
2026-11-10
7 min read

A practical CORS troubleshooting checklist for preflight requests, response headers, credentials, proxies, and local development failures.

A CORS error means the browser rejected a cross-origin response under the server’s declared sharing rules. This troubleshooting guide gives you a repeatable way to read the console and Network panel, identify preflight requests, correct response headers, handle credentials, and separate a genuine CORS problem from an API, proxy, authentication, or deployment failure.

Overview

Cross-origin resource sharing, or CORS, applies when JavaScript running on one origin requests a resource from another 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, as do http://localhost:3000 and http://localhost:4000.

CORS is enforced by browsers. The API may receive a request even when the browser later prevents frontend JavaScript from reading the response. CORS is therefore not an authentication system, an API firewall, or a replacement for authorization checks. Your server must still authenticate users, authorize operations, validate input, and protect sensitive data.

Begin with the browser’s Network panel rather than changing headers at random. Record the request URL, request method, request origin, status code, request headers, response headers, and whether the browser sent an OPTIONS request first. This usually reveals whether the failure is caused by CORS policy or by a separate server problem.

Simple requests and preflighted requests

A browser can send some cross-origin requests without a preflight. A request is more likely to trigger preflight when it uses methods such as PUT, PATCH, or DELETE, sends non-safelisted headers, or uses a content type outside the browser’s safelisted set. A JSON request with Content-Type: application/json commonly causes a preflight.

For a preflight, the browser sends an OPTIONS request containing headers such as:

  • Origin: the page’s origin.
  • Access-Control-Request-Method: the method the browser wants to use.
  • Access-Control-Request-Headers: non-safelisted headers the actual request will include.

The server must answer that preflight with an appropriate status and CORS response headers. If it fails, the browser normally does not send the actual request.

Checklist by scenario

Scenario 1: The browser reports that an origin is not allowed

  1. Copy the exact value of the request’s Origin header. Check the scheme, hostname, and port. A trailing slash is not part of an origin, but http versus https and one port versus another matter.
  2. Compare that value with the server’s allowed-origin configuration. Do not compare only hostnames if your application has separate local, staging, and production origins.
  3. Confirm that the response includes Access-Control-Allow-Origin with either the exact permitted origin or, where appropriate, *.
  4. If the response is cached or served through a CDN, add Vary: Origin when the server returns different CORS responses for different origins.

Do not add a path to Access-Control-Allow-Origin. CORS origins do not include paths, so https://app.example.com/dashboard does not match the origin sent by the browser.

Scenario 2: An OPTIONS preflight fails

  1. Find the OPTIONS request in the Network panel and inspect its status code.
  2. Make sure the server, reverse proxy, or platform route accepts OPTIONS for the API path.
  3. Return Access-Control-Allow-Origin for the requesting origin.
  4. Return Access-Control-Allow-Methods containing the intended method, such as GET, POST, OPTIONS.
  5. Return Access-Control-Allow-Headers containing requested headers such as Content-Type or Authorization.
  6. Ensure authentication middleware does not reject the preflight simply because it has no application token. Preflight handling and actual-request authentication are separate concerns.

A successful preflight does not prove that the application request will succeed. The actual request can still return a 401, 403, 404, 500, or validation error, and that response also needs suitable CORS headers if frontend code must read it.

Scenario 3: Cookies or other credentials are involved

When using cookies, client certificates, or another credentialed mode, the frontend must opt in, for example with fetch(url, { credentials: 'include' }). The server must return Access-Control-Allow-Credentials: true and a specific Access-Control-Allow-Origin value. A wildcard origin cannot be combined with credentialed CORS.

Also check cookie attributes and authentication policy separately. CORS headers do not make a cookie cross-site, and a cookie that is not sent is not necessarily evidence of a CORS failure. Inspect the request’s Cookie header and the browser’s cookie diagnostics before changing server code.

Scenario 4: Local development is blocked

Local frontend and API servers often use different ports, so they are different origins even when both use localhost. You have two common options:

  • Configure the API to allow the exact development origin, such as http://localhost:5173.
  • Use a development-server proxy so browser code calls a same-origin path such as /api, while the development server forwards it to the API.

A proxy can simplify local development, but it does not change production CORS requirements. Test the deployed frontend against the deployed API as well. For broader deployment checks, see the web deployment checklist.

Scenario 5: The API uses Express, Nginx, or a serverless function

In Express, CORS behavior is commonly implemented in middleware. The important detail is not the package name but the resulting response headers and the point in the middleware chain where they are added:

app.use((req, res, next) => {
  const allowed = new Set(['http://localhost:5173', 'https://app.example.com']);
  const origin = req.headers.origin;

  if (origin && allowed.has(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Vary', 'Origin');
    res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  }

  if (req.method === 'OPTIONS') return res.sendStatus(204);
  next();
});

For Nginx, confirm that the relevant location block handles OPTIONS and adds headers on both successful and error responses. A simplified pattern might look like this:

location /api/ {
  if ($request_method = OPTIONS) {
    add_header Access-Control-Allow-Origin "https://app.example.com" always;
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
    add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;
    return 204;
  }

  add_header Access-Control-Allow-Origin "https://app.example.com" always;
  proxy_pass http://api_upstream;
}

Treat this as a configuration pattern to adapt and test, not a drop-in security policy. In serverless functions, put equivalent headers on every response path, including validation errors and exceptions. If a gateway handles OPTIONS before the function runs, configure CORS at the gateway as well.

Reference table: common messages and checks

Browser symptomLikely checkResponse requirement
Origin is not allowedExact scheme, host, and portAccess-Control-Allow-Origin
Preflight response is not successfulOPTIONS route, proxy, redirects, status codeSuccessful OPTIONS response with CORS headers
Method is not allowedRequested method versus server policyAccess-Control-Allow-Methods
Request header is not allowedCompare requested headers with configurationAccess-Control-Allow-Headers
Credential mode is not supportedWildcard origin and cookie settingsSpecific origin plus Access-Control-Allow-Credentials: true
Response cannot be readMissing headers on the actual responseCORS headers on success and error responses

What to double-check

  • Redirects: Inspect whether the API or preflight is redirected from HTTP to HTTPS, between hostnames, or to a login page. Test the final API URL directly and configure the client to use the canonical endpoint.
  • Authorization headers: Adding Authorization often makes a request preflighted. Ensure the preflight allows it, while still validating the token on the actual request. For token inspection and related cautions, see the JWT debugging guide.
  • URL construction: An incorrectly encoded query string can send the request to an unexpected route or alter a parameter. Check the final URL in the Network panel and use the URL encoder and decoder guide when needed.
  • Proxy layers: Identify whether CORS headers are being added by the application, a reverse proxy, an API gateway, or a CDN. Two layers returning conflicting headers can create confusing results.
  • Response visibility: A request can succeed on the server while browser JavaScript receives a CORS error. Compare server logs with browser behavior, but do not treat a server log entry as proof that the frontend can read the response.

Common mistakes

Using mode: 'no-cors' as a fix. This usually produces an opaque response that frontend JavaScript cannot inspect. It does not make a JSON API usable by the application.

Allowing every origin in production without reviewing data exposure. A permissive policy may be acceptable for a genuinely public, read-only resource, but it deserves deliberate review when responses contain user-specific data or credentials.

Fixing only the successful response. Error responses need CORS headers too if the frontend must display the API’s error details. Configure headers consistently across status codes.

Adding headers only in application code. A load balancer or proxy may answer the preflight before application code runs. Trace the complete request path.

Confusing CORS with network failure. DNS errors, TLS failures, connection refusals, timeouts, server errors, and authentication failures can appear alongside CORS messages. First confirm that the endpoint is reachable and returns the expected status.

When to revisit

Keep this checklist with the integration rather than treating CORS as a one-time setup task. Revisit it when the frontend hostname, API hostname, scheme, or port changes; when moving from local development to staging or production; when introducing cookies or an Authorization header; when adding a new HTTP method; or when changing a reverse proxy, gateway, CDN, serverless route, or deployment workflow.

Before a release, test at least one successful request, one preflighted request, an unauthorized request, and a representative error response from the real deployed frontend. Record the expected origins and headers in the project documentation. When a failure returns, start again with the browser’s exact Origin, the preflight exchange, and the final response rather than copying a broad configuration from another project. That workflow is usually faster, safer, and easier to maintain than trial-and-error header changes.

Related Topics

#cors#web-apis#javascript#api-debugging#frontend-development#developer-tools
F

Florence Cloud Editorial Team

Developer Tools Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.