Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource — the fix

Blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource means the browser read the API's response, or its answer to the OPTIONS preflight, and found no permission for your site's origin. After a deploy the usual reason is that the frontend and the API now live at different addresses. Allow the exact frontend origin on the server, answer OPTIONS, keep the headers on error responses, or serve the frontend and the API from one origin.

By Livops team, the people who build and run LivopsUpdated

What Chrome prints in the console[7]:

Access to fetch at 'https://api.example.com/orders' from origin 'https://app.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The same error when it is the preflight that failed[7]:

Access to fetch at 'https://api.example.com/orders' from origin 'https://app.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Firefox says it as Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing)[3][4]. In your JavaScript, fetch simply rejects: for security reasons the details of a CORS failure are not available to code, only in the browser’s console[1].

This page explains what the browser is checking, why the error usually appears the first time the frontend and the API are deployed, and how to fix it in Express, FastAPI and Django — or avoid it by serving both from one origin. It ends with how it looks on Livops.

What does the error mean?

Browsers follow the same-origin policy: a page’s scripts may call fetch() or XMLHttpRequest freely only against the origin the page came from[1]. Two URLs have the same origin if the scheme, host and port all match[2]. https://app.example.com and https://api.example.com are two origins; so are http://localhost:5173 and http://localhost:8000.

CORS is how a server relaxes that rule. For a cross-origin request, the browser looks for Access-Control-Allow-Origin in the response; without it, or with a value that does not match the page’s origin, the response is not handed to the script[1][4]. The header carries either * or a single origin, and a server that serves several frontends has to return the one that made the request[6].

Some requests are sent straight away: GET, HEAD or POST with only CORS-safelisted headers and a Content-Type of application/x-www-form-urlencoded, multipart/form-data or text/plain[1]. Everything else — PUT, DELETE, a JSON body, an Authorization header — is preflighted: the browser first sends an OPTIONS request with Access-Control-Request-Method and Access-Control-Request-Headers, and sends the real request only if the server’s answer allows it[1].

Two things follow. CORS does not protect the server: a request that is not preflighted reaches it and is processed, and CORS only decides whether the page’s JavaScript may read the response[8]. And most CORS errors can only be fixed on the server that answers, not in the frontend[3].

Causes, most common first

1. The frontend and the API are now on different origins

Locally the frontend often called the API through the dev server, or the API allowed http://localhost:5173. After a deploy the page loads from https://app.example.com and calls https://api.example.com, a different host, so the request is cross-origin and needs the header. Subdomains of one domain still count as different origins[2].

2. The allowed list does not contain the exact origin

An origin is written as scheme, host and, if it is not the default, port, with no path and no trailing slash[11]. https://app.example.com/ with a slash, http:// instead of https://, or a missing www. variant all fail to match the Origin the browser sends.

3. The preflight OPTIONS request is not answered

The router has no OPTIONS route and answers 404 or 405, or an auth middleware rejects the preflight for lack of a token. A preflight never carries credentials[1], so an API that demands a token on every request turns away every preflight. Chrome then starts its reason with Response to preflight request doesn't pass access control check, and one of the reasons it gives for a preflight is It does not have HTTP ok status.[7]

4. An error response without the headers

If the app crashes, or a proxy in front of it answers with a 502 page, that response never passes through your CORS code, and the browser reports a missing header instead of the real error. The same happens inside the app when the CORS code runs too late: in Starlette, and so FastAPI, ServerErrorMiddleware is always the outermost layer and produces the 500 for an unhandled exception[10], and django-cors-headers cannot add its headers to responses generated by middleware placed before it[11]. Check the status code of the failing request in the browser’s Network tab first: a 500 or 502 there is the real problem.

5. Credentials with *

With credentials: "include" in fetch (or withCredentials in XMLHttpRequest), the server must name an explicit origin; * is refused, and a Set-Cookie in such a response does not set the cookie either[1][5]. Chrome reports this one with its own message, 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'.[7] A credentialed response also needs Access-Control-Allow-Credentials: true[1].

6. A redirect on the preflight

An API URL written with http:// that redirects to https://, or a path that redirects to add a trailing slash, turns the preflight into a redirect. Not all browsers follow redirects after a preflight[1]; Chrome says Redirect is not allowed for a preflight request.[7] Call the final URL directly.

How to fix it

Allow the frontend’s exact origins, let the middleware answer OPTIONS, and add credentials only if the frontend sends cookies.

Express: the cors package

import express from "express";
import cors from "cors";

const app = express();

app.use(cors({
  origin: ["https://app.example.com", "https://admin.example.com"],
  credentials: true, // only if the frontend sends cookies
}));
app.use(express.json());
// routes after this

Used with app.use() before the routes, the middleware already answers preflight requests for every route[8]. origin takes a string, a RegExp, an array of both, or a function; with a list, the matching request origin is reflected back[8]. credentials: true adds Access-Control-Allow-Credentials[8]. Without options the package sends Access-Control-Allow-Origin: *, its default[8], which is why credentials: true alone is not enough.

FastAPI: CORSMiddleware

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://app.example.com", "https://admin.example.com"],
    allow_credentials=True,  # only if the frontend sends cookies
    allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
    allow_headers=["Authorization", "Content-Type"],
)

The defaults are restrictive: allow_methods is ['GET'] and allow_headers is empty[9]. With allow_credentials=True, none of allow_origins, allow_methods and allow_headers may be ['*']; list them[9]. allow_origin_regex matches many subdomains with one pattern[9]. The middleware answers preflight requests itself and adds the headers to normal responses[9].

To keep the headers on the 500 from an unhandled exception, Starlette recommends wrapping the whole application in CORSMiddleware, so it sits outside ServerErrorMiddleware[10]:

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

api = FastAPI()
# ... routes on api ...

app = CORSMiddleware(
    api,
    allow_origins=["https://app.example.com"],
    allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
    allow_headers=["Authorization", "Content-Type"],
)

Django: django-cors-headers

python -m pip install django-cors-headers
INSTALLED_APPS = [
    # ...
    "corsheaders",
]

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    # ...
]

CORS_ALLOWED_ORIGINS = ["https://app.example.com", "https://admin.example.com"]
CORS_ALLOW_CREDENTIALS = True  # only if the frontend sends cookies

CorsMiddleware goes as high as possible, before any middleware that can return a response, such as CommonMiddleware or WhiteNoise’s; otherwise it cannot add the headers to those responses[11]. One of CORS_ALLOWED_ORIGINS, CORS_ALLOWED_ORIGIN_REGEXES or CORS_ALLOW_ALL_ORIGINS must be set; the last allows every site and the docs call it dangerous[11]. For cookies across sites, CORS_ALLOW_CREDENTIALS is not the whole story: Django’s session cookie is SameSite=Lax by default, which keeps it out of cross-domain requests[11], and a cross-origin POST also meets Django’s CSRF checks — see CSRF verification failed.

Check the preflight with curl

Send the preflight the browser would send, from a terminal:

curl -i -X OPTIONS https://api.example.com/orders \
  -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: authorization,content-type"

A working API answers with a 2xx status, Access-Control-Allow-Origin: https://app.example.com, and the method and headers in Access-Control-Allow-Methods and Access-Control-Allow-Headers[1]. A 3xx means a redirect (cause 6), a 404 or 405 an unhandled OPTIONS (cause 3), a 5xx a crash (cause 4). When the origin is reflected rather than *, the response should also carry Vary: Origin[6].

What does not fix it

  • mode: "no-cors". It turns off the check, but the response becomes opaque: status 0, no headers, a body your code cannot read[3]. It suits beacons, not an API you read.
  • Access-Control-Allow-Origin: * on a private API. MDN’s advice is to keep * for public APIs and name specific origins otherwise[4], and it does not work with cookies anyway[5].
  • Access-Control-Allow-Origin: null. Sandboxed documents and file: pages have the origin null, so anyone can create a page that matches it; MDN says to avoid it[6].

Or avoid CORS: one origin for the frontend and the API

The same-origin policy only restricts requests to another origin[1]. If the page and the API answer on the same scheme, host and port[2] — the frontend at https://app.example.com/, the API at https://app.example.com/api/ — the requests are not cross-origin, so there is nothing for CORS to check and no Access-Control-Allow-Origin to send. The frontend calls relative paths:

const res = await fetch("/api/orders", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(order),
});

Two ways to get there: let the backend serve the built frontend files as well, or put a router in front that sends /api/* to the backend and everything else to the static site. If you need to call an API you do not control, route the call through your own server, which can add the headers you need[3].

On Livops

  • Separate services, separate origins. Every service gets its own address like <name>.livops.app, with HTTPS. A static site and an API deployed as two services are two origins, so a call from one to the other is cross-origin and needs the server settings above — with the site’s https://<name>.livops.app address, and any domain you connect, in the allowed list. See Domains.
  • One repository, one origin. If one repository holds a bot, an API and a Mini App or an admin panel, Livops starts them all in one click: the backend first, then the sites. Requests from the sites to /api reach the backend on their own, so a frontend that calls relative /api/... paths needs no CORS setup. In a project, which paths of a static site another service answers can be read and set with list_site_routes and set_site_routes over MCP, and the assistant can send a static site’s paths to another service.
  • _redirects does not proxy to another site. A 200 rule whose target is https://... is dropped; only redirects (301, 302, 307, 308) may point outside the site. You cannot reach an outside API under your own origin with a rewrite in _redirects. See Redirects and headers.
  • The API address in the frontend. A static site reads variables such as VITE_* or NEXT_PUBLIC_* at build time and bakes them into its files, and a variable can reference another service’s URL. If the site keeps calling the API at its own address, put that address in such a variable and allow the site’s origin on the API. See Variables.
  • Storage buckets have their own CORS. If the blocked request goes to a bucket, set CORS on the bucket’s page: App pages upload allows your project’s addresses, Any site reads allows every site with GET and HEAD only. See Storage buckets.

A static site is free; an app costs 39 credits a month (39,000 so’m). See Pricing.

Questions

Why does the same request work in curl or Postman but fail in the browser?

CORS is enforced by browsers, not by servers. Your API receives and answers the request either way; the browser then refuses to hand the response to your JavaScript because the permission header is missing. curl and Postman do not check it, so they show the response.

Can I fix this error from the frontend?

Mostly no. The permission has to come from the server that answers. mode no-cors only makes the response opaque, so your code cannot read it. If you do not control the API, call it through a server you do control, or put the frontend and the API on one origin.

Is Access-Control-Allow-Origin * safe to use?

Only for a public API that anyone may read. It does not work for requests that send cookies or other credentials, and a private API should list its own frontend origins instead.

Why does it fail only for POST, PUT or requests with an Authorization header?

Those requests are preflighted: the browser first sends an OPTIONS request and waits for permission. If the API does not answer OPTIONS, or answers it without the CORS headers, the real request is never sent.

My CORS settings look right. Why do I still see No Access-Control-Allow-Origin header?

Look at the status of the failing response. A crash, a 502 from a proxy, a 404 for OPTIONS or a redirect often comes back without the headers your CORS code would add, and the browser reports it as a CORS error. Fix that response, or make sure the CORS code runs on it.

Are app.example.com and api.example.com the same origin?

No. An origin is the scheme, host and port together, so two subdomains of one domain are two origins, and so are http and https versions of one host. The API must allow the frontend's exact origin.

How do I avoid CORS on Livops?

Keep the frontend and the backend in one repository and call the backend with relative /api paths. Livops starts the backend first, then the sites, and requests from the sites to /api reach the backend on their own, so the browser sees one origin.

Sources

  1. MDN — Cross-Origin Resource Sharing (CORS) (developer.mozilla.org). Checked .
  2. MDN — Same-origin policy (developer.mozilla.org). Checked .
  3. MDN — CORS errors (developer.mozilla.org). Checked .
  4. MDN — Reason: CORS header 'Access-Control-Allow-Origin' missing (developer.mozilla.org). Checked .
  5. MDN — Reason: Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*' (developer.mozilla.org). Checked .
  6. MDN — Access-Control-Allow-Origin (developer.mozilla.org). Checked .
  7. Chromium source — cors_error_string.cc (raw.githubusercontent.com). Checked .
  8. expressjs/cors — README (github.com). Checked .
  9. FastAPI — CORS (Cross-Origin Resource Sharing) (fastapi.tiangolo.com). Checked .
  10. Starlette — Middleware (starlette.dev). Checked .
  11. django-cors-headers — README (github.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your frontend and API

Got a question? — @livops_uz