SoftwareSideHustles
Debugging

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

text
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 present
on the requested resource.

Variants you may see:

  • Response to preflight request doesn't pass access control check — the OPTIONS request 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 Authorization header or any custom header
  • Methods such as PUT, PATCH and DELETE

The server must answer the preflight with:

http
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 600

A 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)

javascript
import cors from "cors";
app.use(cors({ origin: ["http://localhost:3000", "https://app.example.com"], credentials: true }));

FastAPI

python
from fastapi.middleware.cors import CORSMiddleware
app.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

java
@Bean
CorsConfigurationSource 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:

  1. The client opts in: fetch(url, { credentials: "include" }) (Axios: withCredentials: true).
  2. The server responds with Access-Control-Allow-Credentials: true.
  3. Access-Control-Allow-Origin must be the exact origin, not *.
  4. 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" } in vite.config.js.
  • Next.js: rewrites() in next.config.js mapping /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 /api to 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-Origin in 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 Origin header 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.

Cookies on this site

We use essential cookies to run the questionnaire, practice labs and sign-in. With your permission we'd also like to use analytics to understand which pages help people, and advertising cookies to measure our Google Ads. Rejecting never limits any feature. Cookie policy