# "No 'Access-Control-Allow-Origin' header is present on the requested resource": what it means and how to fix it

**Audit ID:** `errors-in-console` · **Category:** Best Practices

<!--QA-->
> **Quick answer:** "No 'Access-Control-Allow-Origin' header is present on the requested resource" means the browser blocked your script from reading a cross-origin response because the server did not allow your origin. Fix it on that server: send Access-Control-Allow-Origin with your origin on every response, including errors and the OPTIONS preflight. If you do not control it, call the API from your own backend.
<!--/QA-->

This console message means the browser made a request to another origin, got a response, and then refused to hand that response to your JavaScript because the server never said your origin was allowed to read it. The full line in Chrome looks like this:

```
Access to fetch at 'https://api.example.com/items' from origin 'https://www.example.com'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on
the requested resource.
```

The fix lives on the server that answers the request, not in the page that makes it. Lighthouse surfaces the message through the audit "Browser errors were logged to the console" ([`errors-in-console`](/audits/errors-in-console)), and one row fails it. This page covers what the browser checked, why the header is often missing only some of the time, how to test it with curl, and the server configuration that fixes it.

## TL;DR

- **What:** A cross-origin response arrived without an `Access-Control-Allow-Origin` header, so Chrome blocked your script from reading it. CORS stands for Cross-Origin Resource Sharing.
- **Why it matters:** The `fetch()` or XHR call fails for every visitor, so whatever depended on it (a price, a cart, a font, a form) breaks. It also fails the Lighthouse console audit.
- **Fix:** Make the server that owns the URL send `Access-Control-Allow-Origin` with your origin, on every response including errors and the `OPTIONS` preflight. If you do not control that server, call it from your own backend instead.

## What does "No 'Access-Control-Allow-Origin' header is present on the requested resource" mean?

Browsers apply the **same-origin policy**: a script on `https://www.example.com` can send a request anywhere, but it can only read the response if the response comes from the same origin, or if the other server opts in. An origin is the scheme, host and port together, so `https://example.com`, `https://www.example.com` and `http://example.com` are three different origins.

The opt-in is one response header:

```
Access-Control-Allow-Origin: https://www.example.com
```

When the header is absent, Chrome drops the response and prints this message. Three details help when reading it:

| Part of the message | What it tells you |
|---|---|
| `Access to fetch at`, `XMLHttpRequest at`, `font at`, `script at` | What kind of request was blocked |
| `at '<URL>'` | The server that has to change. The fix goes there. |
| `from origin '<origin>'` | The exact value that server has to allow |
| `(redirected from '<URL>')` | The request was redirected and the final URL is the one without the header |
| `Response to preflight request doesn't pass access control check:` | The `OPTIONS` request failed, before the real request was sent |

Chrome usually logs two more lines with it: `Failed to load resource: net::ERR_FAILED` and, if the promise is not caught, `Uncaught (in promise) TypeError: Failed to fetch`. They are the same failure seen from the network layer and from your code.

The request itself often did reach the server. CORS does not stop a request from being sent or processed. It stops the page from reading the answer. Do not treat it as protection for your API.

## Why is the header missing?

The header is missing for one of these reasons, roughly in order of how often they turn up:

1. **The server has no CORS configuration at all.** It was built to be called from its own pages or from other servers, and nobody told it to allow a browser on another origin.
2. **The header is only added to successful responses.** The API allows your origin on a `200`, then returns a `401`, `404`, `500` or `502` without the header. The console shows the CORS message, and the real problem is the error behind it. nginx does this by default: `add_header` skips error responses unless you add `always`.
3. **The preflight is not handled.** For requests with a JSON body, an `Authorization` header or a method such as `PUT` or `DELETE`, the browser first sends an `OPTIONS` request. If the server answers it with a `404`, a `405`, a redirect or a login page, the message carries the preflight wording.
4. **A redirect.** The URL redirects (to add a trailing slash, to `www`, to a login page) and the final response has no header.
5. **The origin does not match the allow list.** The server allows `https://example.com` and the page runs on `https://www.example.com`, or on a preview or staging domain. Servers that check an allow list usually send no header at all for an origin they do not know.
6. **A cache served a response made for someone else.** A CDN cached the response to a request that had no `Origin` header, so the cached copy has no CORS headers. This is common for fonts and images.
7. **A `crossorigin` attribute on a tag.** `<script crossorigin>`, `<img crossorigin>` and `<link rel="preload" as="font" crossorigin>` turn an ordinary request into a CORS request. If the file server does not send the header, a file that used to load now fails.

## How do I test for the header with curl?

Send the request with the `Origin` header your page would send and read the response headers:

```bash
curl -s -o /dev/null -D - -H "Origin: https://www.example.com" https://api.example.com/items
```

Look for `access-control-allow-origin` in the output and check the status line. A `4xx` or `5xx` status means the header is missing because the request failed. Fix that error first.

For a request that needs a preflight, send the `OPTIONS` request the browser would send:

```bash
curl -s -o /dev/null -D - -X OPTIONS \
  -H "Origin: https://www.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type, authorization" \
  https://api.example.com/items
```

A working preflight returns a `2xx` status with `access-control-allow-origin`, `access-control-allow-methods` and `access-control-allow-headers`.

In DevTools, open the Network panel, reload, and click the failed row. A second row of type `preflight` next to it means the `OPTIONS` request is the one to inspect.

## How do I fix it on the server?

The server that owns the URL in the message has to send the header. Pick the narrowest value that works:

| Situation | Header to send |
|---|---|
| Public data, no cookies or auth (fonts, open JSON, images) | `Access-Control-Allow-Origin: *` |
| One known site calls the API | `Access-Control-Allow-Origin: https://www.example.com` |
| Several known sites call it | Echo the request `Origin` if it is on your allow list, and add `Vary: Origin` |
| The request sends cookies or uses `credentials: 'include'` | The exact origin (never `*`), plus `Access-Control-Allow-Credentials: true` |

### nginx

```nginx
location /api/ {
    add_header Access-Control-Allow-Origin "https://www.example.com" always;
    add_header Vary "Origin" always;

    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin "https://www.example.com" always;
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;
        add_header Access-Control-Max-Age 86400 always;
        return 204;
    }

    proxy_pass http://backend;
}
```

The `always` flag is the part most configurations miss. Without it the header vanishes on error responses.

### Apache

```apache
<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "https://www.example.com"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization"
</IfModule>
```

### Node.js with Express

```js
import cors from 'cors';

app.use(cors({
  origin: ['https://www.example.com', 'https://example.com'],
  credentials: true,
}));
```

Register it before the routes and before the auth middleware, so that preflights and `401` responses carry the headers too.

### Static files, fonts and CDNs

For fonts and other public files, send `Access-Control-Allow-Origin: *` from the storage bucket or the CDN. If the origin server varies the header by origin, also send `Vary: Origin`, and purge the cache after the change so that old header-less copies are not served.

## How do I fix it when I do not control the server?

You cannot add a header to someone else's response from the browser. The options are:

- **Call the API from your own backend.** A server-to-server request has no same-origin policy. Add a route on your own origin that fetches the data and returns it, and call that route from the page. This also keeps API keys out of the browser.
- **Use a same-origin rewrite.** A reverse-proxy rule, a Next.js rewrite or an edge function can map `/api/vendor/*` on your domain to the vendor's API.
- **Ask the vendor to allow your origin.** Many APIs have an allowed-origins setting in their dashboard. A missing header is often a setting nobody filled in.
- **Check whether the API is meant for browsers at all.** If the vendor's docs only show server examples, it is not, and the proxy route is the fix.

## Why does `mode: 'no-cors'` not fix it?

Setting `fetch(url, { mode: 'no-cors' })` makes the error go away and gives you an **opaque** response: status `0`, no headers, and a body your code cannot read. It only helps when you do not need the answer, such as firing a tracking beacon. For data you want to use, it changes nothing.

The same goes for browser extensions that turn CORS off and for launching Chrome with web security disabled. They change your browser and nobody else's. Public CORS proxy services do work, but they put a third party in the middle of your traffic, rate-limit you and disappear without notice. Keep them out of production.

## How do I fix it on WordPress, Shopify, or Next.js?

- **WordPress:** the common cases are fonts or scripts loaded from the other form of the domain (`www` versus bare) and REST API calls from a separate front end. Make the Site Address and WordPress Address match the URL visitors use, so that assets are same-origin. For a headless front end, add the headers in the web server config, or with a `rest_pre_serve_request` filter for REST responses.
- **Shopify:** theme JavaScript cannot call most third-party APIs or the Admin API directly. Use an app proxy, which serves the app's endpoint from the shop's own domain, or the Storefront API, which is built for browser calls. If the failing URL belongs to an installed app, report the exact message to the app developer.
- **Next.js:** for a route on your own app that another origin calls, set the headers in the route handler or in `headers()` in `next.config.js`, and export an `OPTIONS` handler. For a third-party API your pages call, add a `rewrites()` entry or a route handler that fetches it on the server.

## How do I verify the fix?

1. Run the curl commands again. Both the real request and the `OPTIONS` request should return `access-control-allow-origin` with your origin.
2. Reload the page with DevTools open. The red row and the three console lines should be gone.
3. Test an error path too: request a URL that returns `404` or send a bad token, and confirm the header is still there. Then you will see the real error instead of a CORS message.
4. Re-run Lighthouse. The row should be gone from "Browser errors were logged to the console". If it was the only error, the audit passes.

## What mistakes should I avoid?

- **Changing the request instead of the response.** Adding `Access-Control-Allow-Origin` to your `fetch()` headers does nothing, and it can trigger a preflight that was not needed. It is a response header.
- **Sending `*` together with credentials.** Browsers reject that combination. Send the exact origin and `Access-Control-Allow-Credentials: true`.
- **Sending two values or two headers.** `Access-Control-Allow-Origin` takes one origin or `*`. If both the app and the proxy add it, the browser sees two and blocks the response with a different message.
- **Echoing any origin back with credentials allowed.** That lets every website read your users' data. Check the origin against an allow list first.
- **Forgetting `Vary: Origin`.** Without it a cache can serve one site's allowed response to another site, and the error comes and goes.
- **Debugging CORS when the request really failed.** Check the status code first. A `500` with no CORS headers is a server error to fix, not a CORS setup to change.

## Related audits

- [Browser errors were logged to the console](/audits/errors-in-console): the audit this message fails, with the other common console errors
- [Failed to load resource: net::ERR_BLOCKED_BY_CLIENT](/audits/err-blocked-by-client): a request the browser cancels before it is sent
- [Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR](/audits/err-http2-protocol-error): a response the server or a proxy cut off
- [Issues were logged in the Issues panel in Chrome DevTools](/audits/inspector-issues): where Chrome also reports CORS and cross-origin policy problems

---

Audit your URL at https://lighthouse-md.com.
