jQuery getJSON 處理json數(shù)據(jù)的代碼
更新時間:2010年07月26日 00:30:52 作者:
Ashx處理程序:如果需要返回json格式的對象,需要把mime類型設置為:"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;//因為mime類型是文本 所以返回回來的是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格式的對象,需要把mime類型設置為:"application/json"。
查看jQuery源文件,可以看出getJSON這樣實現(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);
}
復制代碼 代碼如下:
<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;//因為mime類型是文本 所以返回回來的是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格式的對象,需要把mime類型設置為:"application/json"。
查看jQuery源文件,可以看出getJSON這樣實現(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);
}
您可能感興趣的文章:
- JQuery 獲取json數(shù)據(jù)$.getJSON方法的實例代碼
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結
- jquery $.getJSON()跨域請求
- Jquery getJSON方法詳細分析
- jQuery+ajax中getJSON() 用法實例
- JQuery中getJSON的使用方法
- JQuery中的$.getJSON 使用說明
- ie下jquery.getJSON的緩存問題的處理方法
- jQuery中$.ajax()和$.getJson()同步處理詳解
- 用原生JavaScript實現(xiàn)jQuery的$.getJSON的解決方法
- jQuery中$.get、$.post、$.getJSON和$.ajax的用法詳解
- jQuery使用getJSON方法獲取json數(shù)據(jù)完整示例
相關文章
jQuery數(shù)據(jù)緩存功能的實現(xiàn)思路及簡單模擬
jQuery緩存系統(tǒng)不僅運用于DOM元素,動畫、事件等都有用到這個緩存系統(tǒng)2013-05-05jQuery的實現(xiàn)原理的模擬代碼 -5 Ajax
對于 xhr 對象來說,我們主要通過異步方式訪問服務器,在 onreadystatechange 事件中處理服務器回應的內(nèi)容。簡單的 xhr 使用如下所示。2010-08-08