js执行跨域请求

时间:2023-03-09 06:33:27
js执行跨域请求
   //js执行跨域请求
var _script = document.createElement('script');
_script.type = "text/javascript";
_script.src = "http://api.map.baidu.com/location/ip?ak=Z90WfgUtDDPnr2pfG4yIbkwLEmyoeetx&callback=f";
document.head.appendChild(_script); function f(data){
alert(data.content.point.y);
}

浏览器不会阻止script标签的跨域请求

因此在这里动态添加一个script标签

关键的地方是在链接中对应的callback参数的添加,其中callback=f中的f就是即将执行的回掉函数,其中f(data)中的参数data是执行跨域请求后返回的结果,在页面中的回掉函数中对返回结果进行处理