What Is CORS and Why Does It Block My API Requests?

CORS is a browser security mechanism that blocks cross-origin requests unless the server explicitly allows them. Learn how it works and how to fix common CORS errors.

Published September 20, 2026

CORS (Cross-Origin Resource Sharing) is a browser rule that blocks a web page from reading responses from a different origin unless that server explicitly allows it with Access-Control-Allow-Origin headers. The fix is on the API server, not in your frontend code.

The concept

An origin is scheme + host + port. A page on http://localhost:3000 calling https://api.example.com is cross-origin, so the browser checks permission first.

For non-simple requests (JSON, custom headers, PUT/DELETE) the browser sends a preflight OPTIONS request. The server must answer with the allowed origin, methods and headers; otherwise the real request is never sent.

Preflight then request

fetch()OPTIONS → allow?Pagelocalhost:3000Browserenforces CORSAPIapi.example.com
The server must opt in via response headers.

Practical examples

Express

import cors from 'cors'
app.use(cors({ origin: 'https://myapp.com', credentials: true }))

Raw headers

Access-Control-Allow-Origin: https://myapp.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization

Dev-only proxy (Next.js rewrite)

module.exports = { async rewrites() { return [{ source: '/api/:p*', destination: 'https://api.example.com/:p*' }] } }

Common mistakes

  • Using * together with credentials (not allowed).
  • Trying to fix CORS in frontend code.
  • Forgetting the server must answer OPTIONS.
  • Disabling CORS globally in production.

Key takeaways

  • CORS is browser-enforced and server-configured.
  • Allow specific origins.
  • Handle the preflight OPTIONS request.

FAQ

Why does Postman work but the browser fails?

Postman is not a browser and does not enforce CORS.

Is CORS a security feature for the API?

It protects users' browsers; it does not stop direct API calls.

More HTTP articles