ashx+jsonp+document.referrer

來源:互聯網
上載者:User

標籤:

--

一年前學的JSONP 跨域,一年後的今天相關知識點基本忘光。花了一天時間重新學習,再次感謝各位前輩的文章,特此記錄如下。

--html

<!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>ashx+jsonp+UrlReferrer</title><script type="text/javascript" src="Scripts/jquery-1.12.4.min.js"></script><script type="text/javascript">    $(    function () {        $("#btn").click(function () {            var url = "http://192.168.1.48:6060/Ashx/test_json.ashx";            $.ajax({                type: "get",                async: false,                url: url,                dataType: "jsonp", //資料類型為jsonp                  jsonp: "jsonp", //服務端用於接收callback調用的function名的參數                  jsonpCallback: "callback",//跟ashx的函數名一致                success: function (result) {                    alert(result.id+‘-‘+ result.name + ‘-‘+ result.host);                },                error: function (fail) {                    alert(fail);                }            })        });    })     </script></head><body><div><a href="http://192.168.1.48:6060/Ashx/test.ashx" >點擊跳轉ashx</a><button id="btn" >json</button></div></body></html>

 --ashx

 public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            if (context.Request == null || context.Request.UrlReferrer == null)            {                context.Response.Write("error: not get referrer");                return;            }            else            {                if (context.Request.UrlReferrer.Host.IndexOf("192.168.1.252") > -1)                {                    context.Response.Write("callback({\"id\": 123,\"name\":\"jason\",\"host\":\"" + context.Request.UrlReferrer.Host + "\"})");    // 返回字串前加 callback包裹成JS方法方可解析                }            }        }

 重要知識點:

1、ashx裡面,response輸出的json字串必須包裹在callback()裡面,必須和html的jQuery函數jsonpCallback: "callback" 一致。(非常重要,否則報錯)。

因為jQuery成功解析的字串要求如下:

callback({"id": 123,"name":"jason","host":"192.168.1.252"})

2、json的列名必須用雙引號包裹,如"name",在C#裡加\逸出字元,如\"name\"。

3、document.referrer 是唯讀屬性,可防止盜鏈,偽鏈,一般使用<a href="">跳轉</a>  才不為null,直接使用js跳轉window.open()無效,伺服器後台介面可使用Response.Redirect("ashx_jsonp")。

4、使用jQuery的click方法也可以擷取document.referrer。

 

ashx+jsonp+document.referrer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.