跳到主要内容

浏览器的同源策略

如果两个 URL 的协议、端口(如果有指定的话)和主机都相同的话,则这两个 URL 是同源的。

下表给出了与 URL http://store.company.com/dir/page.html 的源进行对比的示例:

URL 结果 原因
http://store.company.com/dir2/other.html 同源 只有路径不同
http://store.company.com/dir/inner/another.html 同源 只有路径不同
https://store.company.com/secure.html 失败 协议不同
http://store.company.com:81/dir/etc.html 失败 端口不同(http:// 默认端口是 80)
http://news.company.com/dir/other.html 失败 主机不同

不同源通信,就要用到 CORS 通信。

参考链接

https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy