How to fix CORS errors: a practical guide

What CORS errors actually mean, why they happen, and how to fix them correctly on the server, including preflight requests, credentials and common mistakes.

By the UK Technology Hub teamPublished 4 minute read

The short answer

A CORS error means the browser blocked a web page from reading a response from another origin because the server didn't explicitly allow it. CORS is enforced by browsers and configured on the server. Fix it by having the API return the right Access-Control-Allow-Origin and related headers, handle preflight OPTIONS requests, and list allowed methods and headers. Never fix it by allowing every origin with credentials.

"Access to fetch has been blocked by CORS policy" is one of the most common errors in web development. It's also widely misunderstood. CORS isn't a bug in your front-end code; it's a browser security feature that the server must configure.

Why CORS exists

Browsers follow the same-origin policy: a page from one origin (a combination of scheme, domain and port) can't freely read responses from another origin. This stops malicious sites from reading your data from other sites where you're logged in.

Cross-Origin Resource Sharing (CORS) lets servers say, "requests from these origins are allowed to read my responses."

How CORS works

  1. Your page at app.example.com calls an API at api.example.com.
  2. The browser sends the request with an Origin header.
  3. The API responds with Access-Control-Allow-Origin listing allowed origins.
  4. If the origin matches, the browser lets your code read the response. If not, it blocks it and shows a CORS error.

Preflight requests

For requests that aren't "simple," such as those using methods like PUT or DELETE, custom headers or JSON content types, the browser first sends an OPTIONS "preflight" request asking permission. The server must respond with:

  • Access-Control-Allow-Origin: the allowed origin.
  • Access-Control-Allow-Methods: allowed HTTP methods.
  • Access-Control-Allow-Headers: allowed request headers.
  • Access-Control-Max-Age (optional): how long to cache the preflight.

If the server doesn't handle OPTIONS correctly, the real request never happens.

Credentials

If requests include cookies or authentication headers with credentials mode enabled:

  • The server must send Access-Control-Allow-Credentials set to true.
  • Access-Control-Allow-Origin cannot be "*"; it must name the specific origin.

Step-by-step fix

  1. Read the full error in the browser console. It usually says exactly which header is missing or wrong.
  2. Check the Network tab for the preflight request and its response headers.
  3. Configure the server or API gateway to return the right headers for allowed origins.
  4. Handle OPTIONS requests and return a success status with CORS headers.
  5. Include CORS headers on error responses too, otherwise errors appear as CORS failures and hide the real problem.
  6. Check intermediate layers: CDNs, proxies and gateways can strip or override headers.

Common mistakes

  • Trying to fix CORS in front-end code. It can't be fixed there.
  • Using browser extensions that disable CORS, which only hide the problem on your machine.
  • Allowing all origins with "*" on APIs that use cookies or sensitive data.
  • Reflecting any incoming Origin header back without checking it against an allow-list.
  • Forgetting localhost or staging origins during development, or leaving them enabled in production unnecessarily.

When you don't control the API

If a third-party API doesn't allow your origin, call it from your own server or serverless function instead of directly from the browser. This also keeps API keys secret, which you should do anyway.

Same-origin alternatives

Serving your front end and API from the same origin, for example app.example.com and app.example.com/api, avoids CORS entirely. Many hosting platforms make this easy with rewrites or proxies.

Once CORS is working, you might hit rate limits. Our guide to fixing API rate limit 429 errors covers handling those gracefully.

Debugging checklist

  1. Is the request reaching the server at all? Check server logs.
  2. Does the preflight OPTIONS request return a success status?
  3. Does the response include Access-Control-Allow-Origin matching your origin exactly, including protocol and port?
  4. If using cookies, is Access-Control-Allow-Credentials set and the origin specific?
  5. Are custom request headers listed in Access-Control-Allow-Headers?
  6. Is a CDN, proxy or gateway stripping headers?
  7. Do error responses include CORS headers too?

CORS and security

CORS isn't a security barrier for your server; it protects users' browsers. Your API must still authenticate and authorise every request, because non-browser clients ignore CORS entirely. If requests fail in other ways after CORS is fixed, such as memory or performance problems in the front end, our guide to fixing JavaScript memory leaks covers one common cause.

How we help

Our web app development team configures APIs, gateways and CORS securely. Our web development team sets up same-origin routing where it simplifies things, and our app development service designs APIs for web and mobile clients.

Frequently asked questions

Can I fix CORS in JavaScript?

No. CORS is enforced by the browser and configured on the server that receives the request.

Why do mobile apps not get CORS errors?

CORS is a browser security feature. Native mobile apps aren't subject to it, though APIs should still authenticate requests.

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

Only for genuinely public resources that don't use cookies or credentials. Use specific origins for private APIs.

This guide is general information, not legal, tax or financial advice, and details such as prices, features and rules can change. Product and company names mentioned are trademarks of their respective owners and are used only to identify those products; UK Technology Hub is not affiliated with or endorsed by them.

Related guides

How to fix React hydration errors

Why React hydration mismatches happen in server-rendered apps, how to read the error, and reliable fixes for dates, browser-only code, invalid HTML and extensions.

How to fix the WordPress white screen of death

A calm, step-by-step guide to diagnosing and fixing a blank white WordPress page, from recovery mode and debug logs to plugins, themes and memory limits.

All bug fixes & troubleshooting guides