我是靠谱客的博主 重要小白菜,最近开发中收集的这篇文章主要介绍用script标签实现跨域,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

用script标签来实现跨域请求数据 首先接口传数据的格式是var xx = xxxxxxxxxxxxx这样的格式的 动态创建script的标签在src中写入接口连接,在chrome等浏览器的script的回调是onload,ie是onreadystatechange,在这里面处理返回成功回调函数。 具体代码片段 `function AjaxJsonpAsync(url, datas, callback){
var url = url+datas+”&_=” + (new Date()).getTime();
var script = document.createElement(‘script’);
script.setAttribute(‘src’, url);
var agent = navigator.userAgent.toLowerCase();

  if (!(agent.indexOf("msie") > 0)) {
       script.onload = function() {
       callback();
    }
}
    else {
    script.onreadystatechange = function() {
    console.log(this.readyState);        

   console.log(agent);     
       if(this.readyState == "loaded" || this.readyState == "complete"){
         callback();
       }
    }
    }
    document.getElementsByTagName('head')[0].appendChild(script);
}`

最后

以上就是重要小白菜为你收集整理的用script标签实现跨域的全部内容,希望文章能够帮你解决用script标签实现跨域所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部