More JavaScript answers

by Danny
Published 2 min read

The browser blocked a cross-origin response because the server did not allow your origin. Learn how CORS works and how to fix it on the server or with a proxy.

The short answer

#

The server you are calling did not say your origin is allowed to read its response, so the browser blocked it. The fix belongs on the server: send an Access-Control-Allow-Origin header that names your origin. If you do not control the server, call it from your own backend or a dev proxy instead of from the browser.


_10
// Node.js server: allow one known origin
_10
res.setHeader('Access-Control-Allow-Origin', 'https://app.example.com');
_10
res.setHeader('Vary', 'Origin');

Why it happens

#

Browsers apply the same-origin policy: a page on one origin (scheme, host and port) cannot read responses from another origin unless that origin opts in. CORS (Cross-Origin Resource Sharing) is the opt-in. The server lists which origins may read its responses using response headers.

A few points that explain most confusion:

  • The request often does reach the server. The browser just refuses to hand the response to your code.
  • http://localhost:3000 and http://localhost:8080 are different origins because the port differs.
  • Requests with methods other than GET, HEAD or POST, custom headers such as Authorization, or a JSON Content-Type trigger a preflight. The browser first sends an OPTIONS request and only sends the real one if the server answers with the right headers.
  • Tools such as curl and Postman ignore CORS, which is why the same request works there.

How to fix it

#

Send the right headers from the server

For a simple read, the server must return Access-Control-Allow-Origin with your origin. For preflighted requests it must also answer OPTIONS with the allowed methods and headers.


_16
const allowed = new Set(['https://app.example.com']);
_16
_16
function cors(req, res, next) {
_16
const origin = req.headers.origin;
_16
if (allowed.has(origin)) {
_16
res.setHeader('Access-Control-Allow-Origin', origin);
_16
res.setHeader('Vary', 'Origin');
_16
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
_16
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
_16
}
_16
if (req.method === 'OPTIONS') {
_16
res.statusCode = 204;
_16
return res.end();
_16
}
_16
next();
_16
}

If you use Express, the cors package does the same thing with less code.

Sending cookies across origins

To send cookies, the client must opt in and the server must allow credentials. A wildcard origin is not allowed in this case.


_10
fetch('https://api.example.com/me', { credentials: 'include' });

The server then needs Access-Control-Allow-Credentials: true and an exact origin in Access-Control-Allow-Origin.

Use a proxy during development

If the API and front end run on different ports locally, let the dev server forward API calls so the browser sees one origin. In Vite:


_10
// vite.config.js
_10
export default {
_10
server: {
_10
proxy: {
_10
'/api': 'http://localhost:8080',
_10
},
_10
},
_10
};

Then call fetch('/api/users') instead of the full URL.

Call third-party APIs from your backend

If you do not control the API, and it does not support CORS, call it from your own server or a serverless function and return the result. This also keeps any API keys out of the browser.

Common pitfalls

#
  • Using mode: 'no-cors' in fetch. It removes the error but gives you an opaque response you cannot read.
  • Setting Access-Control-Allow-Origin on the request. It is a response header; adding it on the client does nothing.
  • Returning * with credentials. Browsers reject that combination.
  • Forgetting to handle OPTIONS. A server that returns 404 or 401 to the preflight blocks the real request.
  • Missing headers on error responses. If your 500 page has no CORS headers, the browser reports a CORS error and hides the real problem.
#

Was this resource helpful?