zoukankan      html  css  js  c++  java
  • javascript跨域访问

    1.jsonp
        requestpage:
            function jsonpCallback(jsonp)
            {
                alert(jsonp.name);
            }
            function test()
            {
                var ele = document.createElement("script");
                ele.src = "WebForm2.aspx?callback=jsonpCallback";
                document.getElementsByTagName("head")[0].appendChild(ele);
            }
      responsepage:
            protected void Page_Load(object sender, EventArgs e)
            {
                string callback = Request.QueryString["callback"];
                Response.Write(callback+"({'name':'lilei','sex':'男'})");
            }
    2.window.name
        requestpage:
            <iframe id="if1" ></iframe>
            //window.name
            var state = 0;
            function test2() {
                var if1 = document.getElementById("if1");
                window.name = "1";
                if1.src = "WebForm6.aspx";
                if1.onload = IframLoadFn;
     
            }
            var IframLoadFn = function () {
                var if1 = document.getElementById("if1");
                if (state == 1) {
                    var data = window.name;    // 读取数据 if1.contentWindow.name
                    alert(data);
                } else if (state == 0) {
                    state = 1;
                    if1.src = "Proxy.htm";    // 设置的代理文件和请求页面同一个域,此文件不一定要真实存在
                }
            };
        responsepage:
            <script type="text/javascript">
                window.name = "{'name':'lilei','sex':'男'}";
            </script>
    3.iframe
        requestpage:
             <iframe id="if1" ></iframe>
            function test1()
            {
                var if1=document.getElementById("if1");
                if1.src = "WebForm4.aspx?callback=jsonpCallback";
            }
        responsepage:
             <iframe id="pro" ></iframe>
            <script type="text/javascript">
                function _getQuery(key) {//通用方法,获取url参数 
                    var query = location.href.split("?")[1];
                    var value = decodeURIComponent(query.split(key + "=")[1].split("&")[0]);
                    return value;
                }
                var callback = _getQuery("callback");
                var pro = document.getElementById("pro");
                pro.src = "proxy.aspx?callback=" + callback + "&data=" + "{'name':'lilei','sex':'男'}";
            </script>
        proxypage:和requestpage同域
            <script type="text/javascript"> 
                function _getUrl(key) {//通用方法,获取URL参数 
                    var query = location.href.split("?")[1]; 
                    var value = decodeURIComponent(query.split(key + "=")[1].split("&")[0]); 
                    return value; 
                }
                 (function() { 
                     var callback = _getUrl("callback"); 
                     var data = _getUrl("data");                  
                     window.top[decodeURIComponent(callback)](eval('('+decodeURIComponent(data)+')'));
                 })() ;
            </script>
            
  • 相关阅读:
    4.8 C++ typeid操作符
    4.7 C++ dynamic_cast操作符
    tomcat中class和jar的加载顺序(转)
    java Files类和Paths类的用法 (转)
    搭建DUBBO项目解决DUBBO.XML标签报错的问题(转载)
    Maven异常:Could not find artifact
    在docker宿主机上查找指定容器内运行的所有进程的PID
    jmap、jstack、jps无法连接jvm解决办法
    linux中如何查看某一进程的启动时间
    Eureka与ZooKeeper 的比较(转)
  • 原文地址:https://www.cnblogs.com/AlanWinFun/p/5333728.html
Copyright © 2011-2022 走看看