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
OPTIONSresponse 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/405onOPTIONS, redirects (301/302), omitsAccess-Control-Allow-Origin, or forgets a custom header inAccess-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 orcredentials: "include", you cannot use*forAccess-Control-Allow-Origin. You must echo the specific allowedOriginheader and setAccess-Control-Allow-Credentials: true. - Cache preflight with
Access-Control-Max-Age: Setting a cache lifetime (in seconds, such as86400for 24 hours) reduces repeatedOPTIONSround-trips for subsequent requests.
3. Common Traps to Avoid
- Auth middleware blocking
OPTIONSbefore CORS runs: If an authentication guard or token check runs before CORS/preflight handling and returns401or403on unauthenticatedOPTIONSrequests, 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 likeAuthorizationorX-Api-Keyrequires listing those exact header names (or matchingAccess-Control-Request-Headers) in the preflight response. - Forgetting
Vary: Originwhen reflecting dynamic origins: When your server dynamically setsAccess-Control-Allow-Originbased on the request’sOrigin, includeVary: Originso 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?