
ajax跨域请求
1.协议不同
2.端口不同
3.域名不同
不满足以上条件就是跨域
同源策略:同端口协议同域名
解决跨域问题如下
1.jsonp方法
a.动态创建script标签
b.给script标签添加src属性,值为请求的地址
c.将script标签添加到HTML结构中
d.定义请求地址的回调函数,回调函数第一个参数就是后台返回给我们的数据
var _script = document.createElement("script");
_script.src = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=1&cb=callback";
document.body.appendChild(_script);
function callback(res){
console.log(res)
}
最终解决跨域的通用方法
需要后台在响头设置Access-Control-Allow-Origin
值为*或者是localhost:8080
ajax不存在跨域问题,因为是通过浏览器打开的
网友评论