浏览器的同源策略
同源策略 是一个重要的安全策略,它用于限制一个源的文档或者它加载的脚本如何能与另一个源的资源进行交互
源的定义
如果两个 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://news.company.com/dir/other.html | 不同源 | 主机不同 |
允许跨源网络访问的方式
CORS
- 开发环境使用代理
JSONP