同源策略
浏览器设计的一个功能限制
CORS
突破同源策略的一个方法
JSONP
IE时代的补救办法html
(一)同源的定义:前端
window.origin
举例:https://qq.com
和 https://www.baidu.com
不一样源https://baidu.com
和 https://www.baidu.com
也不一样源
(二)同源策略定义git
假设 https://oliver.com/index.html
引用了https://cdn.com/jQuery.js
那么就说jQuery.js是运行在源 https://oliver.com
里
因此jQuery.js只能得到 https://oliver.com
中的数据,而不能获取源 https://cdn.com
中的数据github
(三)关键问题跨域
(四)同源策略的目的浏览器
Access-Control-Allow-Origin: http://xxx.xxx
http://xxx.xxx
访问源A的数据了步骤
假设oliver.com
想要访问db.com
里的数据函数
db.com
中的数据写到/data.js
里oliver.com
用<script>
引用db.com/data.js
/data.js
执行,执行什么呢?oliver.com
提早定义好window.fn
函数/data.js
执行window.fn({data:[...]})
oliver.com
就经过window.fn
获取到了数据window.fn
就是一个回调啊!优势:post
缺点:网站