1. 什么是跨域?
跨域(Cross-Origin) 指的是浏览器的 同源策略(Same-Origin Policy, SOP) 限制了来自不同源(协议、域名或端口不同)的网页 对服务器资源的访问,导致某些请求被浏览器拦截。
(1) 同源策略
浏览器默认允许同源请求,即:
- 协议(
http
/https
) - 域名(
www.example.com
) - 端口(
:80
/:443
)
必须完全一致,才允许访问资源。例如:
同源:
https://example.com:443/index.html 访问 https://example.com:443/api/data ✅ 允许
跨域:
https://example.com:443/index.html 访问 https://api.example.com:443/data ❌ 被拦截
http://example.com/index.html 访问 https://example