同源策略

同源策略,它是由Netscape提出的一个著名的安全策略。它是浏览器最核心也最基本的安全功能,现在所有支持JavaScript 的浏览器都会使用这个策略。

同源,即域名、端口、协议相同。

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

不同源

域名不同

非同源受到的限制:

  • Cookie、LocalStorage、IndexedDB 等存储性内容
  • DOM 节点(dom节点:将html文档视为树结构,以便进行查找和操作)
  • AJAX 请求不能发送
跨域

当协议、子域名、主域名、端口号中任意一个不相同时,都算作不同域。不同域之间相互请求资源,就算作”跨域“。