CORS(跨域资源共享)是一种浏览器安全机制,用于控制来自一个来源的网页是否可以向位于不同来源的服务器发出请求。默认情况下,浏览器会阻止跨域请求;服务器必须通过响应头明确允许这些请求。
同源策略和问题
text
Origin = scheme + host + port. These are DIFFERENT origins:
https://app.example.com → https://api.example.com (different host)
http://localhost:3000 → http://localhost:4000 (different port)
Browser blocks the cross-origin request UNLESS the server sends CORS headers allowing it.
因此, 上的 React 应用调用 上的 API 是跨域的——除非 API 选择允许,否则浏览器会阻止它。
