@FahadM to JavaScriptEnglish • 7 days ago

How do you fix "Response to preflight request doesn't pass access control check" CORS errors?

message-square
0
1
How do you fix "Response to preflight request doesn't pass access control check" CORS errors?
@FahadM to JavaScriptEnglish • 7 days ago

To fix "Response to preflight request doesn't pass access control check", your backend server (or API gateway/reverse proxy) must respond to the browser’s OPTIONS preflight request with a 2xx status code (such as 204 No Content or 200 OK) and matching CORS headers—specifically Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers. Browsers enforce CORS on the server response, so frontend-only changes cannot bypass a missing or misconfigured server header.


1. Why the Browser Sends a Preflight (OPTIONS) Request

Browsers automatically send an HTTP OPTIONS preflight request before cross-origin requests that do not qualify as “simple” requests—for example, requests using PUT, PATCH, or DELETE, sending Content-Type: application/json, or attaching custom headers like Authorization.

  • What the browser checks: It inspects the OPTIONS response headers to confirm the server permits the requesting origin (Origin), HTTP method (Access-Control-Request-Method), and custom headers (Access-Control-Request-Headers).
  • Why the check fails: If the server returns 404/405 on OPTIONS, redirects (301/302), omits Access-Control-Allow-Origin, or forgets a custom header in Access-Control-Allow-Headers, the browser blocks the actual request and logs the preflight error.

2. Configuring Preflight Responses on the Server

Configure your server or framework middleware to allow your frontend origin, methods, and headers on OPTIONS requests, and return an empty 204 No Content response when the method is OPTIONS.

// Express (Node.js) example handling preflight OPTIONS and CORS headers
const express = require("express");
const app = express();

const allowedOrigins = new Set(["[https://app.example.com](https://app.example.com)", "http://localhost:5173"]);

app.use((req, res, next) => {
  const origin = req.headers.origin;

  if (origin && allowedOrigins.has(origin)) {
    res.setHeader("Access-Control-Allow-Origin", origin);
    res.setHeader("Vary", "Origin");
  }

  res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS");
  res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
  res.setHeader("Access-Control-Max-Age", "86400");

  if (req.method === "OPTIONS") {
    return res.sendStatus(204);
  }

  next();
});
  • Avoid Access-Control-Allow-Origin: * with credentials: If your frontend uses cookies or credentials: "include", you cannot use * for Access-Control-Allow-Origin. You must echo the specific allowed Origin header and set Access-Control-Allow-Credentials: true.
  • Cache preflight with Access-Control-Max-Age: Setting a cache lifetime (in seconds, such as 86400 for 24 hours) reduces repeated OPTIONS round-trips for subsequent requests.

3. Common Traps to Avoid

  • Auth middleware blocking OPTIONS before CORS runs: If an authentication guard or token check runs before CORS/preflight handling and returns 401 or 403 on unauthenticated OPTIONS requests, the preflight fails because browsers do not send credentials or custom auth headers in the same way during preflight.
  • Missing custom headers in Access-Control-Allow-Headers: Sending headers like Authorization or X-Api-Key requires listing those exact header names (or matching Access-Control-Request-Headers) in the preflight response.
  • Forgetting Vary: Origin when reflecting dynamic origins: When your server dynamically sets Access-Control-Allow-Origin based on the request’s Origin, include Vary: Origin so proxies and caches do not serve one origin’s header to another origin.
  • Using a proxy in local development: If you cannot control the backend server during local development, route requests through a dev server proxy (such as Vite or Next.js rewrites) so the browser sees a same-origin request.

TL;DR: Fix preflight CORS errors on the server by returning 204/200 for OPTIONS requests along with Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers, and make sure auth middleware does not block OPTIONS before CORS headers are sent.

Which backend stack or reverse proxy (such as Express, FastAPI, Django, .NET, or Nginx) are you using, and are you sending custom headers or credentials?