TL;DR
|
CORS is a browser security rule that controls when a web page from one site can request data from another. If your front end at app.example.com calls an API at api.example.com, the API has to tell the browser that's allowed, or the browser blocks the response.
CORS errors can be especially frustrating, because the request often reaches the server and succeeds, and the browser still refuses to hand over the answer. This guide explains why, what the common error messages mean, and how to fix them properly.
Key takeaway: browsers only let a page read responses from its own origin, unless the other server says otherwise.
An origin is the combination of protocol, domain, and port. Two addresses have the same origin only if all three match:
| Page address | Requested address | Same origin? | Why |
|---|---|---|---|
| https://example.com | https://example.com/api | Yes | Same protocol, domain, and port |
| https://example.com | https://api.example.com | No | Different subdomain |
| https://example.com | http://example.com | No | Different protocol |
| http://localhost:3000 | http://localhost:8000 | No | Different port |
The same-origin policy stops a malicious page from quietly using your logged-in sessions on other sites, for example reading your email from a page you happened to open. CORS is the safe exception: it lets a server say "these other origins are allowed to read my responses".
Key takeaway: the browser asks, and the server answers with headers that say who's allowed. The browser enforces the answer.
When a page makes a cross-origin request, the browser adds an Origin header saying where the page came from. The server responds, and if the response includes the right CORS headers, the browser lets the page read it.
| Header | Sent by | What it means |
|---|---|---|
Origin | Browser | The origin of the page making the request |
Access-Control-Allow-Origin | Server | Which origin may read the response, or * for any |
Access-Control-Allow-Methods | Server | Which request methods are allowed, such as GET or POST |
Access-Control-Allow-Headers | Server | Which request headers the page may send |
Access-Control-Allow-Credentials | Server | Whether cookies and login details may be included |
Access-Control-Max-Age | Server | How long the browser may remember the preflight answer |
For requests that could change data, or that send unusual headers or content types, the browser asks first. It sends an OPTIONS request, called a preflight, asking whether the real request is allowed. Only if the server answers with the right headers does the browser send the real request. A common example is a POST with a JSON body, or any request carrying an Authorization header.
Key takeaway: CORS is enforced by browsers. The server isn't blocking you. The browser is protecting the user.
This catches many people out. The same request that fails in the browser often works perfectly in tools like curl or Postman, because those tools don't enforce CORS. The server may even have processed the request. The browser simply refuses to show the response to the page, because the server didn't say that page's origin was allowed.
That's also why the fix belongs on the server being called. The page making the request can't give itself permission.
Key takeaway: each error message points to a specific missing or wrong header on the server.
| Error message (shortened) | What it means | How to fix it |
|---|---|---|
| No 'Access-Control-Allow-Origin' header is present on the requested resource | The server didn't send any CORS headers | Configure the server to send Access-Control-Allow-Origin with your front end's origin |
| Response to preflight request doesn't pass access control check | The server didn't answer the OPTIONS preflight correctly | Make sure the server responds to OPTIONS requests with the CORS headers, and doesn't require a login for them |
| The value of the 'Access-Control-Allow-Origin' header must not be the wildcard '*' when the request's credentials mode is 'include' | The page sends cookies, and the server allows any origin | Return the specific requesting origin instead of *, and set Access-Control-Allow-Credentials: true |
| Method PUT is not allowed by Access-Control-Allow-Methods | The preflight didn't list the method | Add the method to Access-Control-Allow-Methods |
| Request header field authorization is not allowed by Access-Control-Allow-Headers | The preflight didn't list a header the page sends | Add the header to Access-Control-Allow-Headers |
| Redirect is not allowed for a preflight request | The preflight was redirected, often from http to https or to add a trailing slash | Call the final address directly |
Key takeaway: allow exactly the origins that need access, on the server, using your framework's CORS support.
Many web frameworks have a well-tested way to send CORS headers, so you rarely need to write them by hand:
| Framework | Common CORS support |
|---|---|
| Express (Node.js) | The cors package |
| Django (Python) | The django-cors-headers package |
| Laravel (PHP) | Built-in CORS support, configured in config/cors.php |
| Symfony (PHP) | NelmioCorsBundle |
| Rails (Ruby) | The rack-cors gem |
Whichever you use, a few rules keep CORS secure:
* only for truly public data that doesn't involve logins or cookies.Access-Control-Allow-Origin. That allows every site on the internet. Check it against your list first.During development, a proxy built into many front-end tools can forward API calls through the same origin as your page, which avoids CORS entirely until you deploy. Our guide What is a reverse proxy? explains the idea.
Key takeaway: on Upsun Cloud, your app sets CORS headers for its own responses. Headers for static files can be set in your configuration, and preview environments need their addresses allowed too.
There are two places to set CORS headers on Upsun Cloud:
| Response type | Where CORS headers come from |
|---|---|
| Responses your app generates, such as API calls | Your app, usually through your framework's CORS support |
| Static files served directly, such as fonts or images | The headers setting on a web location in .upsun/config.yaml |
For static files, the Upsun docs on custom headers show how to set headers per location. For example, to let one other site load your fonts:
applications:
myapp:
type: "nodejs:26"
web:
locations:
"/fonts":
root: "public/fonts"
headers:
Access-Control-Allow-Origin: "https://app.example.com"Preview environments add one wrinkle. Each one has its own web address, so a front end in a preview environment calls your API from a different origin than production does. Keep the list of allowed origins in an environment variable rather than in code, and set a different value per environment with an environment-level variable. Our guide What is a preview environment? explains how preview environments get their addresses.
You can deploy on Upsun Cloud free for 15 days, with no card needed, or read the docs on custom headers.
Cross-origin resource sharing. It's the system that lets a server allow web pages from other origins to read its responses.
The browser shows it, but the cause is almost always the server's response: it's missing the headers that would allow the request.
Postman and similar tools don't enforce CORS. Browsers do, to protect users, so only the browser blocks the response.
For public data that doesn't involve logins or cookies, it's fine. For anything involving user accounts, list specific origins instead. Browsers won't accept * when a request includes credentials.
An OPTIONS request the browser sends before certain cross-origin requests, asking the server whether the real request is allowed.
Not properly. The server being called has to allow your origin. During development, a proxy can route calls through your own origin to avoid the issue.