当前位置:编程学习 > JS >>

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

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

通过 HTTP GET 请求载入 JSON 数据。

 

参数

url:为请求的url地址

data:待发送 Key/value 参数。

callback:载入成功时回调函数。

 

 jQuery.getJSON提供了回调函数(callback),该函数有三个参数:responseText,textStatus,XMLHttpRequest,分别代表请求返回的内容、请求状态和XMLHttpRequest对象。

[javascript] 
$.get("Data/GetServiceInfo.aspx",function(responseText,textStatus,XMLHttpRequest){  
 
//responseText:请求返回的内容   
 
//textStatus:请求状态:success、error、notmodified、timeout  
 
//XMLHttpRequest:XMLHttpRequest对象   
 
}); 

$.get("Data/GetServiceInfo.aspx",function(responseText,textStatus,XMLHttpRequest){

//responseText:请求返回的内容

//textStatus:请求状态:success、error、notmodified、timeout

//XMLHttpRequest:XMLHttpRequest对象

});


 

实例(vs2010):

客户端——

[html]
<%@ 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> 

<%@ 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>

服务端——

[csharp] 
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 
 &

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,