DEV Community

Nitesh Patel
Nitesh Patel

Posted on

Understand CORS 🌐 in Easy Way🦾

CORS : Cross-Origin Resource Sharing

Basically CORS is a security feature of web browser that allow or restrict the web page in one domain to access resource from other domain (cross-origin).

For example, when you fetch data from an api, an error occurred. You may have seen the error in the console of the browser. It happens because api doesn't know that the request you're sending is secure or not.
To access the resource, browser need to allow cross-origin request.

Here CORS mechanism comes into picture.

The response we get after sending a request for data from api, contains HTTP header also. This HTTP header has Access-control-allow-origin and Access-control-allow-method.

Preflight Request is sent automatically from browser as a part of CORS mechanism. It is used to check whether the cross-origin(other domain) is safe to execute or not.

After preflight request, actual request is sent to the other domain i.e. cross-origin.

Not every time preflight request is sent.For simple request, browser doesn't sent preflight request.
It is only sent if browser detect that there might be some security concerns.

That's the simple explaination of cors. For in-depth information, MDN docs are best!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay