jQuery getJSON 處理json數(shù)據(jù)的代碼
更新時(shí)間:2010年07月26日 00:30:52 作者:
Ashx處理程序:如果需要返回json格式的對(duì)象,需要把mime類型設(shè)置為:"application/json"。
Html代碼:
<script type="text/javascript" src="/js/jquery-1.4.js"></script>
<script type="text/javascript">
function jsonTest1()
{
$.ajax({
url:"Handler.ashx",
data:{"type":"ajax"},
datatype:"json",
type:"get",
success:function(data)
{
document.getElementById('div1').innerHTML=data;//因?yàn)閙ime類型是文本 所以返回回來(lái)的是json格式的字符串
}
});
}
function jsonTest2()
{
$.getJSON(
'Handler.ashx',
{'type': 'json','name':'qixuejia' }, //類型格式
function(data)
{
for(var i=0;i<data.length;i++)
{
alert(data[i]["UserId"])
}
}
);
}
</script>
<form id="form1" runat="server">
<div id="div1">
</div>
<input type="button" value="jQuery.ajax()" onclick="jsonTest1()"/>
<input type="button" value="jQuery.getJSON()" onclick="jsonTest2()"/>
</form>
Ashx處理程序:如果需要返回json格式的對(duì)象,需要把mime類型設(shè)置為:"application/json"。
查看jQuery源文件,可以看出getJSON這樣實(shí)現(xiàn)的:
getJSON: function( url, data, callback ) {
return jQuery.get(url, data, callback, "json");
},
public void ProcessRequest(HttpContext context)
{
if (context.Request.Params["type"].Equals("ajax"))
{
context.Response.ContentType = "text/plain";
}
else
{
context.Response.ContentType = "application/json";
}
GetInfo(context);
}
public bool IsReusable
{
get
{
return false;
}
}
public void GetInfo(HttpContext context)
{
System.Collections.Generic.List<UserInfo> listUser = UserInfoManage.GetUserInfoBySQL("Select Top 5 * From Userinfo");
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string ResJsonStr = JsonConvert.SerializeObject(listUser, timeConverter);
context.Response.Write(ResJsonStr);
}
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="/js/jquery-1.4.js"></script>
<script type="text/javascript">
function jsonTest1()
{
$.ajax({
url:"Handler.ashx",
data:{"type":"ajax"},
datatype:"json",
type:"get",
success:function(data)
{
document.getElementById('div1').innerHTML=data;//因?yàn)閙ime類型是文本 所以返回回來(lái)的是json格式的字符串
}
});
}
function jsonTest2()
{
$.getJSON(
'Handler.ashx',
{'type': 'json','name':'qixuejia' }, //類型格式
function(data)
{
for(var i=0;i<data.length;i++)
{
alert(data[i]["UserId"])
}
}
);
}
</script>
<form id="form1" runat="server">
<div id="div1">
</div>
<input type="button" value="jQuery.ajax()" onclick="jsonTest1()"/>
<input type="button" value="jQuery.getJSON()" onclick="jsonTest2()"/>
</form>
Ashx處理程序:如果需要返回json格式的對(duì)象,需要把mime類型設(shè)置為:"application/json"。
查看jQuery源文件,可以看出getJSON這樣實(shí)現(xiàn)的:
getJSON: function( url, data, callback ) {
return jQuery.get(url, data, callback, "json");
},
復(fù)制代碼 代碼如下:
public void ProcessRequest(HttpContext context)
{
if (context.Request.Params["type"].Equals("ajax"))
{
context.Response.ContentType = "text/plain";
}
else
{
context.Response.ContentType = "application/json";
}
GetInfo(context);
}
public bool IsReusable
{
get
{
return false;
}
}
public void GetInfo(HttpContext context)
{
System.Collections.Generic.List<UserInfo> listUser = UserInfoManage.GetUserInfoBySQL("Select Top 5 * From Userinfo");
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string ResJsonStr = JsonConvert.SerializeObject(listUser, timeConverter);
context.Response.Write(ResJsonStr);
}
您可能感興趣的文章:
- JQuery 獲取json數(shù)據(jù)$.getJSON方法的實(shí)例代碼
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- jquery $.getJSON()跨域請(qǐng)求
- Jquery getJSON方法詳細(xì)分析
- jQuery+ajax中g(shù)etJSON() 用法實(shí)例
- JQuery中g(shù)etJSON的使用方法
- JQuery中的$.getJSON 使用說(shuō)明
- ie下jquery.getJSON的緩存問(wèn)題的處理方法
- jQuery中$.ajax()和$.getJson()同步處理詳解
- 用原生JavaScript實(shí)現(xiàn)jQuery的$.getJSON的解決方法
- jQuery中$.get、$.post、$.getJSON和$.ajax的用法詳解
- jQuery使用getJSON方法獲取json數(shù)據(jù)完整示例
相關(guān)文章
jquery 實(shí)現(xiàn)復(fù)選框的全選操作實(shí)例代碼
這篇文章主要介紹了jquery 實(shí)現(xiàn)復(fù)選框的全選操作實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01jQuery數(shù)據(jù)緩存功能的實(shí)現(xiàn)思路及簡(jiǎn)單模擬
jQuery緩存系統(tǒng)不僅運(yùn)用于DOM元素,動(dòng)畫、事件等都有用到這個(gè)緩存系統(tǒng)2013-05-05實(shí)用的Jquery選項(xiàng)卡TAB示例代碼
Jquery選項(xiàng)卡想必大家并不陌生,本文為大家介紹個(gè)比較實(shí)用的jquery TAB選項(xiàng)卡,喜歡的朋友可以學(xué)習(xí)下2013-08-08jQuery的實(shí)現(xiàn)原理的模擬代碼 -5 Ajax
對(duì)于 xhr 對(duì)象來(lái)說(shuō),我們主要通過(guò)異步方式訪問(wèn)服務(wù)器,在 onreadystatechange 事件中處理服務(wù)器回應(yīng)的內(nèi)容。簡(jiǎn)單的 xhr 使用如下所示。2010-08-08