networking http ai_generated true

CORS preflight request failed

ID: networking/cors-preflight-failed

Also available as: JSON · Markdown
91%Fix Rate
90%Confidence
55Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

The browser's CORS preflight OPTIONS request was rejected by the server because the required Access-Control-Allow-Origin, Access-Control-Allow-Methods, or Access-Control-Allow-Headers response headers are missing or incorrectly configured. This prevents cross-origin JavaScript requests.

generic

Workarounds

  1. 92% success Configure the server to respond correctly to preflight OPTIONS requests
    1. Handle OPTIONS method on the API endpoint
    2. Return required headers:
       Access-Control-Allow-Origin: https://your-frontend.com
       Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
       Access-Control-Allow-Headers: Content-Type, Authorization
       Access-Control-Max-Age: 86400
    3. Return 204 No Content for OPTIONS requests
    4. For credentials: set Access-Control-Allow-Credentials: true with a specific origin (not *)
    5. In nginx: add_header 'Access-Control-Allow-Origin' '$http_origin' always;
    6. Test with: curl -X OPTIONS -H 'Origin: https://your-frontend.com' -v https://api.example.com/endpoint
  2. 90% success Use a reverse proxy to serve both frontend and API from the same origin
    1. Configure nginx or similar to proxy API requests:
       location /api/ { proxy_pass http://backend:3000/; }
       location / { root /var/www/frontend; }
    2. Both frontend and API now share the same origin, eliminating CORS entirely
    3. This avoids all CORS complexity and preflight overhead
    4. For development, configure webpack-dev-server or vite proxy:
       proxy: { '/api': { target: 'http://localhost:3000' } }

Dead Ends

Common approaches that don't work:

  1. Set Access-Control-Allow-Origin to * while also using credentials mode 95% fail

    The CORS specification explicitly forbids using the wildcard '*' for Access-Control-Allow-Origin when credentials (cookies, authorization headers) are included. Browsers will reject this combination, and the request will still fail with a CORS error.

  2. Disable CORS checks in the browser using command-line flags for production use 90% fail

    Launching Chrome with --disable-web-security only works for local development on a single machine. It cannot be deployed to end users, does not fix the server-side configuration, and creates a significant security vulnerability if used habitually.

Error Chain

Leads to:
Preceded by:
Frequently confused with: