• jsonp 跨域2


    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="author" content="智能社 - zhinengshe.com" />
    <meta name="copyright" content="智能社 - zhinengshe.com" />
    <title>智能社 - www.zhinengshe.com</title>
    <style>
    
    </style>
    <script>
    
    window.onload = function(){
        var oText = document.getElementById("txt1");
        var oBtn  = document.getElementById("btn1");
        
        //http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15657123900
        var url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm"
        oBtn.onclick = function(){
            var oS = document.createElement("script");
            oS.src = url + "?tel=" + oText.value;
            
            
            document.body.appendChild(oS);
            
            
            if(document.addEventListener){
                //高级
                oS.onload = function(){
                     
                    alert(__GetZoneResult_ );    
                };
            
            } else {
                //ie8-
                oS.onreadystatechange = function(){
                    if(oS.readyState){
                        if(oS.readyState == "loaded" ||oS.readyState == "complete"){
                            
                            alert(__GetZoneResult_ );
                        }
                            
                    } 
                };
            }
        };
            
    };
    
    </script>
    
    </head>
    
    <body>
    
    <input id="txt1" type="text" value="15657123920"/>
    <input id="btn1" type="button" value="查询"/>
    <ul id="ul1"> </ul>
    
    </body>
    </html>
    //版权 北京智能社©, 保留所有权利
    
    
    //options url data cbName timeout success error
    function jsonp(options){
        
        options = options || {};
        if(!options.url){
            return;
        }
        options.data = options.data || {};
        options.cbName = options.cbName || "cb";
        options.timeout = options.timeout || 0;
        
        var fnName = ("jsonp"+Math.random()).replace(".","");
        
        options.data[options.cbName] = fnName;
        
        //拼数据
        var arr = [];
        for(var name in options.data){
            arr.push(name + "=" + encodeURIComponent(options.data[name]));
        }
        var str = arr.join("&");
        
        window[fnName] = function(json){
            options.success && options.success(json);
            
            //清理
            oHead.removeChild(oS);
            clearTimeout(timer);
        };
        
        
        //1 创建script
        var oS = document.createElement("script");
        oS.src = options.url + "?" + str;
        var oHead = document.getElementsByTagName("head")[0];
        oHead.appendChild(oS);
        
        //超时
        if(options.timeout){
            var timer = setTimeout(function(){
                options.error && options.error();
                //清理
                window[fnName] =  function(){};
                oHead.removeChild(oS);    
            },options.timeout);
        }
        
    }
    View Code
  • 相关阅读:
    Golang内存管理和垃圾回收
    深入浅出依赖注入及其在抖音直播中的应用
    applications of DFS and BFS
    操作系统上的程序 (什么是程序和编译器)
    安全通告 | 疑似PurpleFox多手段持续实施攻击活动
    日志导致线程Block的这些坑
    聊聊分布式锁
    汇编_action
    操作系统:设计与实现 (2022 春季学期)
    tldr man 查手册效率工具
  • 原文地址:https://www.cnblogs.com/heboliufengjie/p/4716447.html
Copyright © 2020-2023  润新知