Asp.Net+Jquery.Ajax詳解4-$.getJSON

來源:互聯網
上載者:User

目錄(已經更新的文章會有串連,從7月25日開始,每2到3天更新一篇):

Asp.Net+Jquery.Ajax詳解1-開篇(2012.07.25發)

Asp.Net+Jquery.Ajax詳解2-$.Load(2012.07.26發)

Asp.Net+Jquery.Ajax詳解3-$.get和$.post(2012.07.30發)

Asp.Net+Jquery.Ajax詳解4-$.getJSON(2012.07.31發)

Asp.Net+Jquery.Ajax詳解5-$.getScript(2012.08.04發)

Asp.Net+Jquery.Ajax詳解6-$.ajaxSetup(2012.08.06發)

Asp.Net+Jquery.Ajax詳解7-全域Ajax事件(2012.08.09發)

Asp.Net+Jquery.Ajax詳解8-核心$.ajax(2012.08.12發)

Asp.Net+Jquery.Ajax詳解9-serialize和serializeArray(2012.08.15發)

Asp.Net+Jquery.Ajax詳解10-JSON和XML+寫在最後(2012.08.20發,結束啦!)

 

 

jQuery.getJSON(url, [data], [callback])

通過 HTTP GET 請求載入 JSON 資料。

 

參數

url:為請求的url地址

data:待發送 Key/value 參數。

callback:載入成功時回呼函數。

 

 jQuery.getJSON提供了回呼函數(callback),該函數有三個參數:responseText,textStatus,XMLHttpRequest,分別代表請求返回的內容、請求狀態和XMLHttpRequest對象。

$.get("Data/GetServiceInfo.aspx",function(responseText,textStatus,XMLHttpRequest){ //responseText:請求返回的內容 //textStatus:請求狀態:success、error、notmodified、timeout//XMLHttpRequest:XMLHttpRequest對象 });

 

執行個體(vs2010):

用戶端——

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JqueryAjaxGetJson.aspx.cs" Inherits="JqueryAjaxTest.JqueryAjaxGetJson" %><!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 runat="server">        <title>Jquery Ajax Test</title>       <%--引入Jquery庫--%>    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>    <script type="text/javascript">        $(function () {            //為按鈕綁定事件            $("#TestGetJson").bind("click", GetJsonWithCallback);               });        //測試getJSON,使用回呼函數        //注意:get()方法提供了回呼函數(callback),        //該函數有三個參數,分別代表請求返回的內容、請求狀態和XMLHttpRequest對象        function GetJsonWithCallback(event) {            $.getJSON("Data/GetCity.aspx", { "resultType": "json" }, function (responseText, textStatus, XMLHttpRequest) {                $("#result").html("responseText.length:" + responseText.length + "<br/>" + "responseText[0].CityName:" + responseText[0].CityName);                            });        }          </script></head><body>    <form id="form1" runat="server">    <div>            <input id="TestGetJson" type="button" value="測試jquery.getJSON" />                   <div id="result">        </div>    </div>    </form></body></html>

服務端——

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace JqueryAjaxTest.Data{    public partial class GetCity : System.Web.UI.Page    {        private string resultType = "json";        protected void Page_Load(object sender, EventArgs e)        {            //擷取請求的參數            if (!String.IsNullOrEmpty(Request.QueryString["resultType"])) {                resultType = Request.QueryString["resultType"].ToLower()=="html" ? "html" : "json";            }           string  html = GetResult(resultType);           //清空緩衝區           Response.Clear();           //將字串寫入響應輸出資料流           Response.Write(html);           //將當前所有緩衝的輸出發送的用戶端,並停止該頁執行           Response.End();        }        private string GetResult(string resultType) {            string result = "";            if (resultType == "html") {               //返回的html                result = @"<ul><li id=""1"">北京</li><li id=""2"">天津</li></ul>";            }            else if (resultType == "json") {                //返回的json資料                result = @"[{""pkid"":""0001"",""ProvinceId"":""BJ"",""CityName"":""北京"",""CityNameEn"":""Beijing"",""PostCode"":""010"",""isHotCity"":false}, {""pkid"":""0002"",""ProvinceId"":""TJ"",""CityName"":""天津"",""CityNameEn"":""Tianjin"",""PostCode"":""022"",""isHotCity"":false}]";                              }            return result;        }    }}

 

你可能會有疑問,什麼是JSON?和XML有什麼區別?

 

雖然XML已在不少應用程式中大顯身手,但它並不是十全十美的,特別是遇到AJAX應用的時候,XMLHttpRequest會檢查返回資料的MIME類型,如果是text/xml類型,XMLHttpRequest就會運行XML Parser來解析返回的文檔,並在記憶體中構建出對應的DOM樹,之後,你可以用JavaScript標準的DOM方法來操作DOM樹。由於眾所周知DOM的詬病,這顯然不是有效率的方法。另外一個問題是,如果你想使用JavaScript對象而不是直接用XML資料的話,你還得自己遍曆整個DOM樹來建立相應對象。

 

於是JSON出現在我們面前。

 

JSON提供了一種更適合AJAX應用的標準資料交換格式。JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式。易於人閱讀和編寫。同時也易於機器解析和產生。

 

更多內容,將在《Asp.Net+Jquery.Ajax詳解10-JSON和XML》中提到,敬請期待。

 

相關文章

聯繫我們

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