What is “CORS”? And why you should care about it?

Have you ever encountered an error message that read Access to XMLHttpRequest has been blocked by CORS policy If so, you might have been confused about what CORS is and why it's blocking your request.

Recently at work, I've faced a similar CORS-related error, and I know firsthand how frustrating it can be when your code isn't working as expected due to cross-origin restrictions. However, understanding how CORS works is crucial for building secure web applications that can communicate with external resources.

In this post, We'll dive into the basics of CORS, including why it exists, and how it works.

By the end of this post, I hope you'll have a better understanding of CORS and feel more confident in your ability to work with cross-origin requests in your web applications. So, let's get started!

Before diving directly into the technical concepts, let's understand it with a simple example.

Imagine that you are Batman and your Batcave is your website. The Batcave contains all the tools and resources that you need to fight crime, just like your website contains all the files and data that you need to display content to your users.

However, sometimes you need to access external resources, like the Batcomputer or the Batmobile, to help you fight crime more effectively. These external resources are like APIs or images that your website needs to access to display content or perform certain functions.

Now, let's say that you want to access the Batcomputer from your Batcave. You need to make sure that only you, Batman, can access the Batcomputer and that no one else can use it without your permission. In the same way, you want to make sure that your website can only access external resources that it is authorized to use and that no one else can use those resources without your permission.

This is where CORS comes in. In our Batman and Batcave example, CORS is like the security system that protects the Batcomputer and ensures that only Batman can use it.

Just like the Batcomputer only responds to commands from Batman, external resources only respond to requests from authorized domains. If a domain tries to access an external resource without proper authorization, the CORS security system will block the request, just like the Batcomputer would ignore commands from anyone other than Batman.

( I am learning how to become good at drawing )

What is CORS?

Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers that restrict web pages from making requests to a different domain than the one that served the original content. This security feature is implemented to prevent malicious websites from stealing or manipulating user data.

What does CORS do?

CORS specifies a set of rules that enable browsers to make cross-domain requests safely. When a web page makes a request to a different domain, the browser first sends an HTTP request to the server to check if the server allows cross-origin requests. The server responds with a set of headers that specify which domains are allowed to make cross-origin requests.

If the server allows the request, the browser sends the request with an additional header called the "origin" header, which specifies the domain of the web page making the request. The server checks the "origin" header and responds with a set of headers that allow the browser to access the requested resource.

Why you should care about it?

You should care about CORS because it affects how your web pages interact with other domains. If your web page needs to make cross-domain requests, you need to ensure that the server you are making the request to allows cross-origin requests. If the server does not allow cross-origin requests, your web page will not be able to access the requested resource.

CORS is an essential security feature that helps prevent malicious websites from stealing or manipulating user data. As a frontend developer or backend developer, it is important to understand how CORS works and how to implement it correctly to ensure the security of your web pages.

In conclusion, CORS is a security feature that restricts web pages from making requests to a different domain than the one that served the original content. It is important to understand how CORS works and how to implement it correctly to ensure the security of your web pages.

If you found this post valuable hit that like button and share it with your developer friends!

You can connect with me on LinkedIn and Twitter

Hit that follow button for more development-related content!