CORS
一句话解释
CORS 是浏览器 security mechanism 那个 controls 哪个 cross-原点请求是 允许从 web pages.
用大白话说
CORS (Cross-Origin Resource Sharing) is a security feature enforced by browsers. When JavaScript on one origin (e.g., app.com) makes a request to a different origin (e.g., api.com), the browser checks if the server allows it via CORS headers.
Without CORS, any website could make requests to any API on behalf of the user — a security risk. CORS requires the server to include Access-Control-允许-原点 in the response, specifying which origins are allowed. If the header is missing or does not match, the browser blocks the response.
For "non-simple" requests (PUT, DELETE, custom headers), the browser sends a "preflight" OPTIONS request first to check permissions. CORS is configured on the server, not the client — it is not something you fix in your frontend code.
示例
// Server response headers:
Access-Control-Allow-Origin: https://app.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization服务器 CORS 请求头允许请求从 app.com 使用特定方法 and 请求头.
相关术语
相关资源
相关课程
- CORS
学习 CORS.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。