---
title: "Access to fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present"
description: "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."
url: https://www.danny.engineering/answer/javascript/access-to-fetch-has-been-blocked-by-cors-policy-no-access-control-allow-origin-header-is-present
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Javascript"]
---

# Access to fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present

## 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.


```javascript
// Node.js server: allow one known origin
res.setHeader('Access-Control-Allow-Origin', 'https://app.example.com');
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.


```javascript
const allowed = new Set(['https://app.example.com']);

function cors(req, res, next) {
  const origin = req.headers.origin;
  if (allowed.has(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Vary', 'Origin');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  }
  if (req.method === 'OPTIONS') {
    res.statusCode = 204;
    return res.end();
  }
  next();
}
```


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.


```javascript
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:


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


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.

## Related reading

- [MDN: Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS)
- [MDN: Access-Control-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Origin)
