Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
CORS (Cross-Origin Resource Sharing) is security mechanism in web development that allows or prohibits web browsers from making requests to servers on different domains. This is important for ensuring web application security, especially when application interacts with resources on third-party servers.
When web browser tries to make request to server on different domain, it must check if this is allowed by server. This check is called "policy of where resources can be obtained from".
CORS was introduced to solve "cross-domain requests" problem in web browsers. Web browsers by default don't allow web pages to make requests to resources on other domains for security reasons. This prevents attacks such as Cross-Site Request Forgery (CSRF) and Cross-Site Scripting (XSS).
For example, if web page from domain example.com tries to request data from server another-domain.com, browser will by default block such request unless server another-domain.com has indicated that it allows access from other domain.
When browser sends request from different domain (e.g., via AJAX or Fetch), it automatically adds Origin header that indicates domain from which request was made. Server receiving request checks this header and decides whether to allow access to its resources from this domain.
Origin Header:
This is header sent with request that indicates domain from which request was made. Server must check this header and decide whether to allow access.
Server Response:
If server allows access from certain sources, it sends Access-Control-Allow-Origin headers in response. This header indicates which domains can access data.
Example response header:
Access-Control-Allow-Origin: https://example.com
This means access to resource is allowed only from domain example.com.
Request Types:
Preflight request:
If request uses non-standard methods (e.g., PUT, DELETE) or headers, browser first sends OPTIONS request (preflight request) to server to find out if server allows such request.
Simple request:
If request uses standard methods (GET, POST, HEAD) and standard headers, server can immediately send permission.
Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Origin: https://example.com