Sobes.tech
Junior — Middle

Could you explain what is meant by Cross-Origin Resource Sharing (CORS)?

sobes.tech AI

Answer from AI

Cross-Origin Resource Sharing (CORS) is a security mechanism in web browsers that allows controlling access of web pages to resources located on another domain (origin). By default, browsers block requests from one domain to another (same-origin policy) to prevent attacks like Cross-Site Request Forgery.

CORS works through special HTTP headers that the server sends in response, indicating which domains can access its resources. For example, if the server responds with the header Access-Control-Allow-Origin: https://example.com, then only pages from the example.com domain can access this resource via JavaScript.

Example header:

Access-Control-Allow-Origin: *

means that the resource is accessible to all domains.

Thus, CORS allows for secure data exchange between different domains, while the browser blocks unwanted requests.