html跨域访问
html的ajax访问,存在跨域问题,访问非本域的接口时
会出现无法访问的问题。
掌握了html跨域访问,就可以实现前后端分离开发,并且可以通过jsonp构建 是构建 mashup(webAP糅合、混搭模式)
html跨域的两种实现方式;
1.通过创建script 进行get请求数据
function crossDomain(url,params,fn){
var head=document.getElementsByTagName("head")[0];
//1.处理回调函数
var cbName="jsonp"+(Math.random()*Math.random()).toString().substr(2)+new Date().getTime();
/*将回调函数挂载到window对象上*/
window[cbName]=function(data){
//拿到并处理数据
fn(data);
//拿到数据后remove掉
//head.removeChild(scriptObj);
}
//2、解析ur