js跨域及解決方案

1.什麼是跨域javascript

咱們常常會在頁面上使用ajax請求訪問其餘服務器的數據,此時,客戶端會出現跨域問題.html

跨域問題是因爲javascript語言安全限制中的同源策略形成的.java

簡單來講,同源策略是指一段腳本只能讀取來自同一來源的窗口和文檔的屬性,這裏的同一來源指的是主機名、協議和端口號的組合.jquery

例如:web

 

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.實現原理ajax

在HTML DOM中,Script標籤是能夠跨域訪問服務器上的數據的.所以,能夠指定script的src屬性爲跨域的url,從而實現跨域訪問.json

例如:跨域

 

這種訪問方式是不行的.可是以下方式,倒是能夠的.安全

<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

相關文章
相關標籤/搜索