zoukankan      html  css  js  c++  java
  • js跨域解决方案(转载)

    1.什么是跨域

    我们经常会在页面上使用ajax请求访问其他服务器的数据,此时,客户端会出现跨域问题.

    跨域问题是由于javascript语言安全限制中的同源策略造成的.

    简单来说,同源策略是指一段脚本只能读取来自同一来源的窗口和文档的属性,这里的同一来源指的是主机名、协议和端口号的组合.

    例如:

    URL说明是否允许通信
    http://www.a.com/a.js 
    http://www.a.com/b.js
    同一域名下 允许
    http://www.a.com/lab/a.js 
    http://www.a.com/script/b.js
    同一域名下不同文件夹 允许
    http://www.a.com:8000/a.js 
    http://www.a.com/b.js
    同一域名,不同端口 不允许
    http://www.a.com/a.js 
    https://www.a.com/b.js
    同一域名,不同协议 不允许
    http://www.a.com/a.js 
    http://70.32.92.74/b.js
    域名和域名对应ip 不允许
    http://www.a.com/a.js 
    http://script.a.com/b.js
    主域相同,子域不同 不允许
    http://www.a.com/a.js 
    http://a.com/b.js
    同一域名,不同二级域名(同上) 不允许(cookie这种情况下也不允许访问)
    http://www.cnblogs.com/a.js 
    http://www.a.com/b.js
    不同域名 不允许

    2.实现原理

    在HTML DOM中,Script标签是可以跨域访问服务器上的数据的.因此,可以指定script的src属性为跨域的url,从而实现跨域访问.

    例如:

    这种访问方式是不行的.但是如下方式,却是可以的.

    <script src=”http://192.168.0.5/Web/web1.aspx” type="text/javascript"></script>

    这里对返回的数据有个要求,即:服务器返回的数据不能是单纯的如{"Name":"zhangsan"}

    如果返回的是这个json字符串,我们是没有办法引用这个字符串的.所以,要求返回的值,务必是var json={"Name":"zhangsan"},或json({"Name":"zhangsan"})

    为了使程序不报错,我们务必还要建立个json函数.

    3.解决方案

    方案一

    服务器端:

            protected void Page_Load(object sender, EventArgs e)
            {
                string result = "callback({"name":"zhangsan","date":"2012-12-03"})";
    
                Response.Clear();
                Response.Write(result);
                Response.End();
            }

    客户端:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
    
            var result = null;
            window.onload = function () {
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = "http://192.168.0.101/ExampleBusinessApplication.Web/web2.aspx";
    
                var head = document.getElementsByTagName("head")[0];
                head.insertBefore(script, head.firstChild);
    
            };
    
            function callback(data) {
                result = data;
            }
    
            function b_click() {
                alert(result.name);
            }
        </script>
    </head>
    <body>
        <input type="button" value="click me!" onclick="b_click();" />
    </body>
    </html>

    方案二,通过jquery来完成

    通过jquery的jsonp的方式.使用此方式,对服务器端有要求.

    服务器端如下:

            protected void Page_Load(object sender, EventArgs e)
            {
                string callback = Request.QueryString["jsoncallback"];
    
                string result = callback + "({"name":"zhangsan","date":"2012-12-03"})";
    
                Response.Clear();
                Response.Write(result);
                Response.End();
            }

    客户端:

    $.ajax({ 
                    async: false, 
                    url: "http://192.168.0.5/Web/web1.aspx", 
                    type: "GET", 
                    dataType: 'jsonp', 
                    //jsonp的值自定义,如果使用jsoncallback,那么服务器端,要返回一个jsoncallback的值对应的对象. 
                    jsonp: 'jsoncallback', 
                    //要传递的参数,没有传参时,也一定要写上 
                      data: null, 
                    timeout: 5000, 
                    //返回Json类型 
                      contentType: "application/json;utf-8", 
                    //服务器段返回的对象包含name,data属性. 
                    success: function (result) { 
                        alert(result.date); 
                    }, 
                    error: function (jqXHR, textStatus, errorThrown) { 
                        alert(textStatus); 
                    } 
                });

    实际上,在我们执行这段js时,js向服务器发出了这样一个请求:

    http://192.168.0.5/Web/web1.aspx?jsoncallback=jsonp1354505244726&_=1354505244742 

    而服务器也相应的返回了如下对象:

    jsonp1354506338864({"name":"zhangsan","date":"2012-12-03"})
    此时就实现了跨域范文数据的要求.
    原文地址:http://www.cnblogs.com/oneword/archive/2012/12/03/2799443.html
    这个人不懒,写了一点东西
  • 相关阅读:
    x64 平台开发 Mapxtreme 编译错误
    hdu 4305 Lightning
    Ural 1627 Join(生成树计数)
    poj 2104 Kth Number(可持久化线段树)
    ural 1651 Shortest Subchain
    hdu 4351 Digital root
    hdu 3221 Bruteforce Algorithm
    poj 2892 Tunnel Warfare (Splay Tree instead of Segment Tree)
    hdu 4031 Attack(BIT)
    LightOJ 1277 Looking for a Subsequence
  • 原文地址:https://www.cnblogs.com/AnonymouL/p/5703759.html
Copyright © 2011-2022 走看看