Access-Control-Allow-Origin Header
What you'll learn: How the server controls which websites can access its resources using the Access-Control-Allow-Origin response header.
When a browser makes a cross-origin request (like fetching data from a different domain), it checks the server's response for permission. The Access-Control-Allow-Origin header is how the server tells the browser, "Yes, this origin is allowed to read my response."
Think of it like a bouncer at a club checking a guest list. Your website (the origin) sends a request, and the server's response includes this header specifying which origins are on the "allowed" list. If your origin matches, the browser lets your JavaScript access the response data.
The Three Common Values
Specific origin:
Access-Control-Allow-Origin: https://example.com
Only https://example.com can access the resource.
Wildcard (any origin):
Access-Control-Allow-Origin: *
Any website can access the resource. This works for simple requests but has limitations with credentialed requests.
No header: If the server doesn't send this header at all, the browser blocks access by default—your JavaScript cannot read the response.
How the Browser Uses It
After receiving the response, the browser compares the Access-Control-Allow-Origin value to your page's origin. If they match (or the value is *), access is granted. If not, you'll see a CORS error in the console, even though the server processed the request successfully.
The server controls this behavior—you cannot bypass it from client-side JavaScript. This is a security feature protecting servers from unauthorized cross-origin access.
Key Takeaway: The Access-Control-Allow-Origin header in the server's response determines which origins can access the resource; the browser enforces this policy by blocking access when origins don't match.