CORS Explained: Why Your Browser Blocks Requests and How to Fix It Properly

Understand the same-origin policy, how CORS headers and preflight requests work, and how to fix the classic “blocked by CORS policy” error safely.

Share on Linkedin Share on WhatsApp

Estimated reading time: 7 minutes

Article image CORS Explained: Why Your Browser Blocks Requests and How to Fix It Properly

Almost every web developer runs into it sooner or later: your front-end calls an API, the request looks fine, and the browser console shows a red message saying the request was “blocked by CORS policy.” The same request works perfectly in Postman or with curl, which makes the error even more confusing.

The good news is that CORS follows clear, predictable rules. Once you understand why browsers enforce it and which headers control it, the error stops being mysterious. This article walks through the concepts step by step.

First, the same-origin policy

To understand CORS, you need to know what it relaxes: the same-origin policy. This is a security rule built into browsers that limits how a page loaded from one origin can read responses from another origin.

An origin is the combination of three parts: scheme (protocol), host and port. Two URLs have the same origin only if all three match.

Compared with https://app.example.comSame origin?Reason
https://app.example.com/profileYesOnly the path is different
http://app.example.comNoDifferent scheme
https://api.example.comNoDifferent host
https://app.example.com:8443NoDifferent port

Why does this matter? Imagine you are logged in to your bank in one tab and visit a malicious site in another. Without the same-origin policy, scripts on that site could send requests to your bank, with your cookies attached, and read the responses. The policy prevents that kind of data theft.

What CORS actually is

Modern applications often need legitimate cross-origin requests. A front-end hosted at app.example.com may need data from api.example.com. CORS (Cross-Origin Resource Sharing) is the standard mechanism that lets a server declare, through HTTP headers, which other origins are allowed to read its responses.

Two points are essential:

  • The browser enforces CORS, not the server. The server only sends headers; the browser decides whether your JavaScript gets access to the response.
  • Tools like curl and Postman ignore CORS. They are not browsers running untrusted page scripts, so they have no reason to apply the rule. That is why a request can “work in Postman” but fail in the browser.

The key response headers

When a page makes a cross-origin request, the browser adds an Origin header describing where the request came from. The server can then answer with CORS headers such as these:

HeaderWhat it does
Access-Control-Allow-OriginThe origin allowed to read the response, or * for any origin
Access-Control-Allow-MethodsHTTP methods permitted for cross-origin requests (used in preflight responses)
Access-Control-Allow-HeadersCustom request headers the client may send (used in preflight responses)
Access-Control-Allow-CredentialsWhether the browser may include cookies or other credentials
Access-Control-Max-AgeHow long the browser can cache the preflight result

If the response does not include an Access-Control-Allow-Origin value that matches the requesting origin (or a wildcard, when allowed), the browser blocks your code from reading it and logs the CORS error.

Simple requests vs. preflight requests

Not every cross-origin request is handled the same way. Some are considered simple requests and are sent directly. In general, a request is simple when it:

  • Uses GET, HEAD or POST
  • Only sets a limited set of standard headers
  • Uses a Content-Type of application/x-www-form-urlencoded, multipart/form-data or text/plain

Anything else, such as a PUT or DELETE request, a JSON body with Content-Type: application/json, or a custom header like Authorization, triggers a preflight request. Before sending the real request, the browser sends an OPTIONS request asking the server whether the actual request is allowed.

The flow looks like this:

  1. The browser sends OPTIONS with headers such as Origin, Access-Control-Request-Method and Access-Control-Request-Headers.
  2. The server responds with the allowed origin, methods and headers.
  3. If everything matches, the browser sends the real request.
  4. The real response must also include the appropriate Access-Control-Allow-Origin header.

A frequent bug is a server that handles the main route but returns an error, a redirect or no CORS headers for the OPTIONS request. In that case, the real request is never sent.

Credentials and the wildcard trap

By default, cross-origin fetch calls do not send cookies. To include them, the client must opt in (for example, credentials: "include" in fetch), and the server must respond with Access-Control-Allow-Credentials: true.

There is an important restriction here: when credentials are involved, Access-Control-Allow-Origin cannot be *. The server must return the specific origin. This rule prevents any website on the internet from making authenticated requests on behalf of your users and reading the results.

How to fix CORS errors the right way

Since CORS is controlled by the server’s response, the real fix almost always happens on the server or in the infrastructure in front of it. A practical checklist:

  • Read the full console message. It usually says exactly which header is missing or which value did not match.
  • Allow specific origins. Keep an allowlist of trusted front-end origins instead of reflecting any incoming Origin header.
  • Handle OPTIONS requests. Make sure preflight requests reach your CORS logic and receive a successful response.
  • List the methods and headers you actually use. If your client sends Authorization, the server must allow it.
  • Use your framework’s tools. Most back-end frameworks offer CORS middleware that handles these details consistently.
  • Use a development proxy when appropriate. Front-end dev servers can proxy API calls so the browser sees a single origin during local development.

Avoid browser extensions or flags that disable web security. They hide the problem on your machine while your users still see the error.

A common misconception

CORS is not a way to protect your API from being called. Any non-browser client can still send requests to your server. CORS protects users by controlling which websites can read responses inside their browsers. Your API still needs proper authentication, authorization and input validation.

Conclusion

CORS errors look intimidating, but they come from a simple idea: browsers keep origins isolated unless a server explicitly says otherwise. Know what an origin is, recognize when a preflight is triggered, configure the right headers on the server and be careful with credentials. With those pieces in place, cross-origin requests become routine.

Want to strengthen your foundations in HTTP, APIs and front-end development? Explore the free web development courses on Cursa and keep building real projects.

Video Courses

Free video courses

See all video courses

Blog

Related articles

Read more about Web Development