同源
“同源"指的是"三個相同”
- 協議相同
- 域名相同
- 端口相同
舉例來說,http://www.example.com/dir/page.html這個網址,協議是http://
,域名是www.example.com
,端口是80
(默認端口可以省略)
示例:
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 | 失敗 | 主機不同 |
不同源的限制
- Cookie、LocalStorage 和 IndexDB 無法讀取。
- DOM 無法獲得。
- AJAX 請求不能發送。
參考
https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy
http://www.ruanyifeng.com/blog/2016/04/same-origin-policy.html
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS