What is CORS (cross-origin resource sharing)?
A mechanism that allows requests to other origins, and the reading of their responses, which the same-origin policy restricts, within the scope the server permits. The server uses response headers such as Access-Control-Allow-Origin to state which origins may read the response. For requests that a simple form submission cannot produce, such as a POST with Content-Type application/json, the browser first sends a check with the OPTIONS method (a preflight). It is a rule enforced by the browser, not a mechanism that protects the server from attacks.
Japanese name: CORS(オリジン間リソース共有)
CORS (cross-origin resource sharing) appears in 1 practice question across 1 topic, including Web Technology Basics.
Field: Web Technology Basics
Simple requests and preflights
GET requests and POST requests in formats a form can send (application/x-www-form-urlencoded, multipart/form-data, text/plain) are sent without a check, and Access-Control-Allow-Origin only decides whether the script may read the response. For requests with other methods or Content-Types, or with custom headers, the browser first sends OPTIONS and states the method and headers it will use in headers such as Access-Control-Request-Method. If the server returns permission, the browser sends the actual request.
FAQ
Can a CORS error be fixed on the client side?
No. The server decides which origins may read its responses, so the API server must add the appropriate permission headers to its responses. For an API you do not manage, another option is to call it through your own server.
How are the same-origin policy and CORS related?
The same-origin policy in principle forbids reading responses from other origins, and CORS is the mechanism for allowing exceptions within the scope the server explicitly states. If the server returns no CORS permission, the same-origin policy's restriction applies as is.