How to Fix CORS Errors: "No 'Access-Control-Allow-Origin' header is present"
What CORS actually is, why the browser blocks your request while Postman works, how preflight requests and credentials change the rules, and the correct server-side fix for Express, FastAPI, Flask, Spring and Django.
By Software Side Hustles Editorial Team · Published · Updated · 4 min read
Quick answer: a CORS error means the server did not tell the browser that your page's origin is allowed to read its response. The fix belongs on the server (or a proxy): send Access-Control-Allow-Origin with your front-end's origin, and answer the OPTIONS preflight request. Nothing in your fetch call can fix it — mode: "no-cors" only hides the response from you.
The error message
Access to fetch at 'http://localhost:8000/api/orders' from origin 'http://localhost:3000'has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is presenton the requested resource.Variants you may see:
Response to preflight request doesn't pass access control check— theOPTIONSrequest failed or lacked headers.The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'— you are sending cookies.Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response— the server must list that header.
What CORS is (and why Postman works)
An origin is scheme + host + port. http://localhost:3000 and http://localhost:8000 are different origins, as are https://app.example.com and https://api.example.com.
Browsers apply the same-origin policy: JavaScript on one origin may send requests to another, but may not read the response unless the server opts in with CORS headers. This protects users — otherwise any site you visit could read your webmail with your cookies.
Postman, curl and server-side code are not browsers and do not enforce CORS. That is why the API "works everywhere except the browser". The request often does reach your server; the browser just refuses to hand the response to your script.
Simple requests and preflight requests
For "simple" requests — GET, HEAD or POST with basic content types such as form data — the browser sends the request and checks the response headers.
Anything else triggers a preflight: an automatic OPTIONS request asking permission first. Common triggers:
Content-Type: application/json- An
Authorizationheader or any custom header - Methods such as
PUT,PATCHandDELETE
The server must answer the preflight with:
Access-Control-Allow-Origin: http://localhost:3000Access-Control-Allow-Methods: GET, POST, PUT, DELETEAccess-Control-Allow-Headers: Content-Type, AuthorizationAccess-Control-Max-Age: 600A frequent bug: authentication middleware rejects the OPTIONS request (which never carries credentials) with a 401, so every preflight fails. Let preflight requests through before authentication.
Server-side fixes by framework
Express (Node.js)
import cors from "cors";app.use(cors({ origin: ["http://localhost:3000", "https://app.example.com"], credentials: true }));FastAPI
from fastapi.middleware.cors import CORSMiddlewareapp.add_middleware( CORSMiddleware, allow_origins=["http://localhost:3000", "https://app.example.com"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)Flask: from flask_cors import CORS; CORS(app, origins=[...], supports_credentials=True).
Django: install django-cors-headers, add its middleware near the top of MIDDLEWARE, and set CORS_ALLOWED_ORIGINS = ["http://localhost:3000"].
Spring Boot
@BeanCorsConfigurationSource corsConfigurationSource() { var config = new CorsConfiguration(); config.setAllowedOrigins(List.of("http://localhost:3000")); config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); config.setAllowedHeaders(List.of("Content-Type", "Authorization")); config.setAllowCredentials(true); var source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/api/**", config); return source;}With Spring Security, also call http.cors(Customizer.withDefaults()), otherwise the security filter rejects preflights before your configuration is consulted.
Cookies and credentials
To send cookies or HTTP authentication cross-origin:
- The client opts in:
fetch(url, { credentials: "include" })(Axios:withCredentials: true). - The server responds with
Access-Control-Allow-Credentials: true. Access-Control-Allow-Originmust be the exact origin, not*.- The cookie itself must be allowed cross-site:
SameSite=None; Secure— and modern browsers increasingly block third-party cookies altogether, so same-site deployment (see below) is more robust.
Avoiding CORS entirely: proxy through the same origin
If the front-end and API are yours, the simplest fix is often to make them the same origin:
- Vite:
server.proxy = { "/api": "http://localhost:8000" }invite.config.js. - Next.js:
rewrites()innext.config.jsmapping/api/:path*to the backend, or route handlers that call it server-side. - Production: serve both behind one domain with a reverse proxy (nginx, a load balancer, a CDN) routing
/apito the backend.
The browser then sees a same-origin request, and no CORS headers are needed.
Things that look like fixes but are not
mode: "no-cors"— the request is sent, but the response becomes opaque: status 0, no body. Your code cannot read it.- Browser extensions or
--disable-web-security— they make it work on your machine only. - Setting
Access-Control-Allow-Originin the request — it is a response header; the browser ignores it on requests. Access-Control-Allow-Origin: *with credentials — the browser rejects that combination.- Reflecting any
Originheader back with credentials allowed — that lets any website make authenticated requests as your users. Use an allowlist.
When the CORS error is really a different error
If the server crashes and returns a 500 page without CORS headers, the browser reports a CORS error, hiding the real problem. Open the Network tab, click the failed request and look at the status code; check the server logs. Similarly, a 404 from a wrong URL or an HTML error page from a proxy appears as a CORS failure. Many frameworks only add CORS headers to successful responses — configure error responses to include them as well.
Frequently asked questions
Can I fix CORS from the front-end?
No. Only the server (or a proxy you control) can grant access. Front-end changes can only avoid the cross-origin request.
Why does CORS fail on localhost?
Different ports are different origins. localhost:3000 calling localhost:8000 is cross-origin.
Does CORS protect my API from other servers?
No. CORS controls what browsers let scripts read. Anyone can call your API with curl. Use authentication and authorisation for protection.
Where this skill pays off
A good answer to "how do I fix my CORS error?" explains the server-side fix; a dangerous one says mode: "no-cors" or "allow every origin with credentials". Telling those apart — and explaining why — is exactly what AI evaluation platforms pay developers for. See common mistakes when reviewing AI-generated code.