从一个域上加载的脚本不允许访问另外一个域的文档属性。
协议、域名、端口同一
Ajax跨域 可以突破 Javascript的安全限制。
实现跨域
JSONP<script>标签可以加载跨域的javascript脚本 <script>标签可以实现和服务端的数据交互。
对比URL 结果 结果
http://www.csdn.com/dir/page2.html 同源 相同的协议,主机,端口
http://www.csdn.com/dir2/other.html 同源 相同的协议,主机,端口
http://username:password@www.example.com/dir2/other.html 同源 相同的协议,主机,端口
http://www.csdn.com:81/dir/other.html 不同源 相同的协议,主机,端口不同
https://www.csdn.com/dir/other.html 不同源 协议不同
http://en.csdn.com/dir/other.html 不同源 不同主机
http://csdn.com/dir/other.html 不同源 不同主机(需要精确匹配)
http://v2.www.csdn.com/dir/other.html 不同源 不同主机(需要精确匹配)
http://www.csdn.com:80/dir/other.html 看情况 端口明确,依赖浏览器实现
jquery ajax解决写法
$.ajax({
type:"get",
url:`https://api.douban.com/v2/movie/subject/${id}`,
async:true,
success:(data)=>{
console.log(data)
},
dataType:"jsonp",
jsonpCallback:"aaa"//这里随便写,必填
})