是一種約定,它是瀏覽器最核心也最基本的安全功能,Web是構建在同源策略基礎之上的,瀏覽器是針對同源策略的一種實現。php
以前一直認爲,它是經過對發出的請求進行檢查是否同源,而後決定是否對該請求加以限制來實現。此次通過驗證發現正好相反:ajax
在www.test.com下的頁面中向www.domian.com下的a.php發送ajax請求:跨域
$.ajax({ type:"get", url:"http://www.domain.com/a.php", async:true, success:function(res){ console.log(res) }, error:function(){ console.log("error") } });
控制檯信息顯示跨域不被容許:瀏覽器
XMLHttpRequest cannot load http://www.domain.com/a.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.test.com' is therefore not allowed access.
查看服務器access.log記錄,找到了這條請求的記錄: 安全