是一种约定,它是浏览器最核心也最基本的安全功能,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记录,找到了这条请求的记录: 安全