zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Feign原理 (图解)
    纠错:Feign 没用 短连接
    【转】linux 查看哪些进程用了swap
    【转】交换分区SWAP
    【改】linux中分区的概念
    【转】SPI FLASH与NOR FLASH的区别 详解SPI FLASH与NOR FLASH的不一样
    【转】DDR3和eMMC区别
    ARP (地址解析协议)
    【转】一文搞懂C语言回调函数
    【改】shell 判断文件中有无特定子串方法(grep)
  • 原文地址:https://www.cnblogs.com/heboliufengjie/p/4716447.html
Copyright © 2011-2022 走看看