所謂同源策略,指的是瀏覽器對不一樣源的腳本或者文本的訪問方式進行的限制。好比源a的js不能讀取或設置引入的源b的元素屬性。那麼先定義下什麼是同源,所謂同源,就是指兩個頁面具備相同的協議,主機(也常說域名),端口,三個要素缺一不可。git
看下面的比較就一目瞭然了:github
URL1 | URL2 | 說明 | 是否容許通訊 |
https://github.com/LoveCatDog/StudyOne | https://github.com/LoveCatDog/cms-admin-mall | 協議、域名、端口都相同 | 容許 |
https://github.com/LoveCatDog/cms-admin-mall | https://github.com:8080/LoveCatDog/cms-admin-mall | 協議、域名相同、端口不一樣 | 不容許 |
http://github.com/LoveCatDog/cms-admin-mall | https://github.com/LoveCatDog/cms-admin-mall | 域名相同、端口相同、協議不一樣 | 不容許 |
https://github.com/LoveCatDog/cms-admin-mall | https://cnblogs.com/LoveCatDog/cms-admin-mall |
協議、端口相同、域名不一樣 | 不容許 |
https://github.com/LoveCatDog/cms-admin-mall | https://www.github.com/LoveCatDog/cms-admin-mall | 協議、端口相同、主域名相同、子域名不一樣 | 不容許 |
同源策略限制的不一樣源之間的交互主要針對的是js中的XMLHttpRequest等請求,寫代碼的時候也經常會引用其餘域名的js文件,樣式文件,圖片文件什麼的,這些則不會收到限制瀏覽器