Ajax跨域 取值 Jsonp的定義注意事項

來源:互聯網
上載者:User

標籤:

今天要做一個去之前的項目上取資料,打算建一個介面,WebServer。中間遇到了一些問題。就是跨域取值的問題

 用戶端頁面

1.首先Ajax請求的DataTy:‘jsonp‘這種格式,還要加一個 jsonp: "callback",最為主要的是有一個 回呼函數Callback(),可把我折騰壞了。直接上用戶端代碼:

<script type="text/javascript">        $(function () {            $.ajax({                type: "GET",                url: "http://localhost:48748/WebService1.asmx/GetGrid",                data: "",                dataType: "jsonp",                jsonp: "callback",                success: function(data) {                                       callback(data);                },                error: function (error) {                    console.log(error);                }            })        })        function callback(data) {                          alert(data.rows[0].Name);             }    </script>

 後台代碼。

 2.採用的webserver 伺服器,具體配置就不多說了,今天的主題是跨域訪問json資料,需要注意的問題就是前面提到的,需要用一個CallBack回呼函數,要有一定的格式。請看代碼:

[WebMethod]        public void GetGrid()        {            List<Notice> list = new List<Notice>();            list.Add(new Notice { Id=1,Name="xiaoming"});            JavaScriptSerializer json = new JavaScriptSerializer();            var grid = new { rows = list };            Context.Response.Write("callback("+json.Serialize(grid)+")");        }

 

這點東西搞了一天,    寫於:2015年1月27日01:06:09

Ajax跨域 取值 Jsonp的定義注意事項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.