• Docs
  • Talk to an expert
Blog
Blog
BlogProductCase studiesNewsInsights
Blog

What is CORS? Errors explained and how to fix them

APIsecurityJavaScript
25 November 2026
Share

TL;DR

  • CORS, short for cross-origin resource sharing, is how a server tells a browser which other websites may call it.
  • By default, browsers block a page from reading responses from a different origin, meaning a different domain, protocol, or port. CORS is the controlled way to allow it.
  • The server allows access by sending headers such as Access-Control-Allow-Origin in its responses.
  • For some requests, the browser first sends a check, called a preflight request, to ask permission.
  • CORS errors appear in the browser, but the fix is almost always on the server that's being called.
  • Fix CORS by allowing only the origins that need access. Avoid turning it off in the browser or allowing everyone when logins or cookies are involved.

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.

The same-origin policy

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 addressRequested addressSame origin?Why
https://example.comhttps://example.com/apiYesSame protocol, domain, and port
https://example.comhttps://api.example.comNoDifferent subdomain
https://example.comhttp://example.comNoDifferent protocol
http://localhost:3000http://localhost:8000NoDifferent 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".

How CORS works

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.

HeaderSent byWhat it means
OriginBrowserThe origin of the page making the request
Access-Control-Allow-OriginServerWhich origin may read the response, or * for any
Access-Control-Allow-MethodsServerWhich request methods are allowed, such as GET or POST
Access-Control-Allow-HeadersServerWhich request headers the page may send
Access-Control-Allow-CredentialsServerWhether cookies and login details may be included
Access-Control-Max-AgeServerHow long the browser may remember the preflight answer

Preflight requests

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.

Why the error appears in the browser

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.

Common CORS errors and how to fix them

Key takeaway: each error message points to a specific missing or wrong header on the server.

Error message (shortened)What it meansHow to fix it
No 'Access-Control-Allow-Origin' header is present on the requested resourceThe server didn't send any CORS headersConfigure the server to send Access-Control-Allow-Origin with your front end's origin
Response to preflight request doesn't pass access control checkThe server didn't answer the OPTIONS preflight correctlyMake 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 originReturn the specific requesting origin instead of *, and set Access-Control-Allow-Credentials: true
Method PUT is not allowed by Access-Control-Allow-MethodsThe preflight didn't list the methodAdd the method to Access-Control-Allow-Methods
Request header field authorization is not allowed by Access-Control-Allow-HeadersThe preflight didn't list a header the page sendsAdd the header to Access-Control-Allow-Headers
Redirect is not allowed for a preflight requestThe preflight was redirected, often from http to https or to add a trailing slashCall the final address directly

How to fix CORS properly

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:

FrameworkCommon 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:

  • List the specific origins that need access. Use * only for truly public data that doesn't involve logins or cookies.
  • Don't simply copy whatever origin a request claims back into Access-Control-Allow-Origin. That allows every site on the internet. Check it against your list first.
  • Don't fix CORS by turning it off in your browser or with a browser extension. It only hides the problem on your machine, and your users will still see the error.
  • Remember that CORS protects users' browsers, not your server. It doesn't stop scripts or other servers from calling your API, so the API still needs its own authentication.

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.

CORS on Upsun Cloud

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 typeWhere CORS headers come from
Responses your app generates, such as API callsYour app, usually through your framework's CORS support
Static files served directly, such as fonts or imagesThe 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.

Frequently asked questions

What does CORS stand for?

Cross-origin resource sharing. It's the system that lets a server allow web pages from other origins to read its responses.

Is a CORS error a server error or a browser error?

The browser shows it, but the cause is almost always the server's response: it's missing the headers that would allow the request.

Why does my request work in Postman but not in the browser?

Postman and similar tools don't enforce CORS. Browsers do, to protect users, so only the browser blocks the response.

Is Access-Control-Allow-Origin: * safe?

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.

What is a preflight request?

An OPTIONS request the browser sends before certain cross-origin requests, asking the server whether the real request is allowed.

Can I fix CORS on the front end?

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.

Stay updated

Subscribe to our monthly newsletter for the latest updates and news.

Deploy possibility.
Try Upsun for free.

Build with DispatchDeploy on Cloud