Cloud Explainers

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

APIs9 min video

'Blocked by CORS policy: No Access-Control-Allow-Origin header' means the request reached your API and the answer came back, but the browser kept it from your page, because the answer didn't say this page's origin may read it. With a real Chromium, nginx and a small API on Ubuntu 24.04 we show the header that fixes it, why the star fails with cookies, the preflight request, Content-Type not allowed, and the same error hiding a 409.

Data in curl, blocked in the browser

Your browser says blocked by CORS policy: No Access-Control-Allow-Origin header. This is a surf school's booking page, and the lessons never load. The page only says Failed to fetch.

Data in curl, blocked in the browser: browser screenshot from the video

The full message is in the browser's developer console, which opens with F12. Now ask the same address with curl, a command-line tool. Every lesson comes back. The data is fine. The only one who can't see it is the page it was built for. The browser blocked it. So did the request even reach the API? Take a guess. Yes. The log shows the browser's request, answered two hundred, OK.

Chromium console, then Ana's server
Access to fetch at 'http://api.surf.test/lessons' from origin 'http://www.surf.test' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Failed to load resource: net::ERR_FAILED
Chromium console, then Ana's server
curl -s http://api.surf.test/lessons[{"id":1,"when":"Sat 9:00","what":"Beginner group","spaces":0},{"id":2,"when":"Sat 11:00","what":"Beginner group","spaces":3},{"id":3,"when":"Sun 10:00","what":"Private lesson","spaces":1}]
Chromium console, then Ana's server
sudo tail -n 2 /var/log/nginx/api.log[20/Nov/2026:12:38:26 +0000] "GET /lessons" 200 origin=http://www.surf.test
[20/Nov/2026:12:38:27 +0000] "GET /lessons" 200 origin=-

So the request got there, and the answer came back, but the browser kept it from the page. The fix is one header on the server, and you'll add it in the next minute. Later, you'll see this same error come back on a server whose settings are right.

Who keeps the answer?: diagram of Booking page, The browser, Lessons API

The quick fix: one header

Meet Ana. She runs Driftwood Surf School's server: the page at www.surf.test and its API at api.surf.test, both served by nginx, a web server. Ask the API the way a browser does, with an Origin header. That header says which site the page came from. Look at the answer: nothing in it starts with Access-Control. So she adds one line to the API's nginx settings, checks them, and reloads. Now the answer carries Access-Control-Allow-Origin, star: any website may read this.

Ana on the surf school's server
curl -sI -H "Origin: http://www.surf.test" http://api.surf.test/lessonsHTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Fri, 20 Nov 2026 12:38:27 GMT
Content-Type: application/json
Content-Length: 190
Connection: keep-alive
Ana on the surf school's server
grep -n add_header /etc/nginx/sites-available/surfschool.conf15:        add_header Access-Control-Allow-Origin "*";
Ana on the surf school's server
sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reload the booking page, and the lessons are there. Star is fine for public data like this. It stops being enough the moment a request carries the customer's sign-in, which is where most people get stuck next.

The quick fix: one header: browser screenshot from the video

Who blocks it, and why

So why would a browser hide an answer that already arrived? It's protecting something called an origin. A page's origin is three things: the scheme, the host name, and the port. www.surf.test and api.surf.test differ in the host name, so they are two origins, even on one server. On your own laptop, a page on localhost port three thousand and an API on port eight thousand are two origins too. By default, a script may only read answers from its own origin. Otherwise any site you visit could quietly read your email or your bank account, using the sign-ins already in your browser.

An origin: scheme + host name + port: diagram of http://www.surf.test, http://api.surf.test, localhost:3000, localhost:8000, On the internet, On your laptop

So here's the whole trip. The page's script calls fetch, and the browser sends the request with the page's origin attached. The API answers with every lesson. Then the browser looks for one header in the answer: Access-Control-Allow-Origin, naming that origin, or star. Here, that header is missing, so the browser keeps the answer away from the page. curl is not a browser, so nobody checks.

Who blocks a CORS request: diagram of Page's script, The browser, Lessons API, curl, Maya's browser, Ana's server

Think of a school at home time. A grown-up at the gate asks for a child. The child is right there, but the teacher only lets them go to names on the pickup list, and the parents wrote that list, not the teacher. The grown-up is the page's script, and the child is the data. The parents are the API's owner, and the API's header is the pickup list. The browser is the teacher. So you fix the list, on the server, never at the gate.

Like a school's pickup list: comparison table of At the school gate vs CORS, Who asks, What's there, Who checks, The list, The fix, The API's owner writes the list

Two fixes that don't work

Two fixes you'll find online don't work. The first is setting mode to no-cors in your fetch call. The red error goes away, but so does your data: your script gets an empty, unreadable answer, opaque, with status zero. The second puts Access-Control-Allow-Origin on the request. It's a response header: the server has to send it. On a request, it makes the browser ask the server's permission first, and the server says no. You'll see that asking step in a minute.

Chromium console: www.surf.test
status: 0  type: opaque  body: ""

The request before the request

Now Maya, a customer who's signed in, books Saturday at eleven. Booking failed. The console blames something called a preflight request, and it blames that star. Star isn't allowed when the request carries credentials, like a sign-in cookie: any website could then read a signed-in customer's bookings.

The request before the request: browser screenshot of a preflight request failed

And in Ana's API log, one line: an OPTIONS request, answered two hundred. That two hundred answered the browser's question, but with the star, so the browser stopped. The booking itself never arrived.

Chromium console, then Ana's server
Access to fetch at 'http://api.surf.test/bookings' from origin 'http://www.surf.test' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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'.
Failed to load resource: net::ERR_FAILED
Chromium console, then Ana's server
sudo tail -n 3 /var/log/nginx/api.log[20/Nov/2026:12:38:32 +0000] "OPTIONS /bookings" 200 origin=http://www.surf.test

That OPTIONS request is the preflight. A plain read, like fetching the lessons, goes straight out. A booking sends its details as JSON, a text format for data, and says so in a Content-Type header. That value isn't on the browser's short list of safe ones. So the browser first asks: may this origin send a POST with that header? Only if the answer says yes does the real request go.

A preflight, then the booking: diagram of Page, Browser, Lessons API, GET /lessons: goes straight, fetch POST, JSON, Content-Type: JSON, OPTIONS: POST + Content-Type?, Allow-Origin, Allow-Headers, POST /bookings + cookie

MDN's guide to CORS says the same. For a preflighted request, the browser first sends options, to find out if the real request is safe to send. And with credentials, the server must name the origin, not use star.

The request before the request: browser screenshot from the video

So Ana swaps the star for the page's exact origin, and adds Access-Control-Allow-Credentials, true. That lets the page read answers to requests that carry cookies. Maya tries again, and gets a new message: the Content-Type header is not allowed. The preflight asked about it, and the answer didn't list it. So she adds Access-Control-Allow-Headers, Content-Type.

Ana's server, then the Chromium console
grep -n add_header /etc/nginx/sites-available/surfschool.conf15:        add_header Access-Control-Allow-Origin "http://www.surf.test";
16:        add_header Access-Control-Allow-Credentials "true";
Ana's server, then the Chromium console
Access to fetch at 'http://api.surf.test/bookings' from origin 'http://www.surf.test' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
Failed to load resource: net::ERR_FAILED

Maya books again, and it works: booked, Saturday at eleven.

The request before the request: browser screenshot from the video

In the log, the preflight, then the POST, two oh one, created. Two requests for one click: the question, then the booking.

Ana on the surf school's server
sudo tail -n 2 /var/log/nginx/api.log[20/Nov/2026:12:38:38 +0000] "OPTIONS /bookings" 200 origin=http://www.surf.test
[20/Nov/2026:12:38:38 +0000] "POST /bookings" 201 origin=http://www.surf.test

The same error, from a full lesson

Then Maya tries Saturday at nine, which is full. The API answers four oh nine, conflict, with a message saying so. Guess first: what does the page show? Booking failed, and the console has the very first error again: no Access-Control-Allow-Origin header. This is the case I promised at the start: the same error, on a server whose settings are right.

The same error, from a full lesson: browser screenshot from the video

That console line is the first error, word for word. Ana's log says four oh nine: the lesson is full. Ask with curl, the same POST with the same Origin and cookie, and the answer has the message, but not one Access-Control line. In nginx, add header only works on success answers, like two hundred and two oh one. Error answers get no Access-Control lines at all, unless you add the word always.

Chromium console, then Ana's server
Access to fetch at 'http://api.surf.test/bookings' from origin 'http://www.surf.test' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Failed to load resource: net::ERR_FAILED
Chromium console, then Ana's server
sudo tail -n 2 /var/log/nginx/api.log[20/Nov/2026:12:38:40 +0000] "POST /bookings" 409 origin=http://www.surf.test
Chromium console, then Ana's server
curl -si -X POST -H "Origin: http://www.surf.test" -b session=maya-7f3a -H 'Content-Type: application/json' -d '{"lesson": 1}' http://api.surf.test/bookingsHTTP/1.1 409 CONFLICT
Server: nginx/1.24.0 (Ubuntu)
Date: Fri, 20 Nov 2026 12:38:41 GMT
Content-Type: application/json
Content-Length: 29
Connection: keep-alive

{"error":"Sat 9:00 is full"}

So Ana adds always to each line, and reloads nginx. Now error answers carry the headers too.

Headers on every answer, errors too: code change of surfschool.conf (api), add_header Access-Control-Allow-Origin "http://www.surf.test" always; (add_header skips 4xx and 5xx without it)

Same click, and the page can finally read the API's answer: Saturday at nine is full.

The same error, from a full lesson: browser screenshot from the video

Check it from a terminal

The console shows you only the error, not the preflight's answer. So after a change, check it yourself with curl: the OPTIONS method, an Origin, and two request headers that ask: may I use POST, and may I send Content-Type? Dash i prints the answer's headers. The answer should name your page's origin, say credentials true if you send cookies, and list every header you send.

Ana on the surf school's server
curl -si -X OPTIONS -H "Origin: http://www.surf.test" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: content-type" http://api.surf.test/bookingsHTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Fri, 20 Nov 2026 12:38:44 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Allow: POST, OPTIONS
Access-Control-Allow-Origin: http://www.surf.test
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type

No nginx in front of your API? Then the same headers come from the app, usually through a small CORS add-on for your framework: Flask, Express, Django, and the rest all have one. The rules are exactly the ones you just saw, and the browser checks them the same way.

Same headers, wherever they come from: cards of nginx, In the app, Flask, Express, Django, Same rules

Recap

No header? Add Access-Control-Allow-Origin on the server. Cookies? Name the origin, and allow credentials. Content-Type not allowed? List it in Allow-Headers. Still blocked, but only when the API returns an error? Send the headers on errors too: in nginx, always. And skip no-cors.

The browser reads the answer's headers: diagram of No header, Cookies, Header not allowed, Only on errors, mode: no-cors

If your page can read its API again, a like helps the next person stuck on this error find it, and subscribing brings more fixes, shown on real machines.

Every message in this video came from a real Chromium, and every command and the lab files are linked in the description. Thanks for watching Cloud Explainers.

Sources

Demos: a container on our own Mac standing in for a surf school's web server, and Playwright's Chromium; the surf school is made up. Every console message was Chromium's own. Watch the full video on YouTube.

More videos

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Go (Golang)5 min

Golang Pointers in 5 Minutes

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

Networking9 min

How TCP Sockets Work

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python10 min

Python uv vs pip: Which One Should You Use?

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index