jQuery getJSON 處理json數(shù)據(jù)的代碼
更新時間:2010年07月26日 00:30:52 作者:
Ashx處理程序:如果需要返回json格式的對象,需要把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;//因為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類型設(shè)置為:"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);
}
復(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;//因為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類型設(shè)置為:"application/json"。
查看jQuery源文件,可以看出getJSON這樣實現(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方法的實例代碼
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- jquery $.getJSON()跨域請求
- Jquery getJSON方法詳細(xì)分析
- jQuery+ajax中g(shù)etJSON() 用法實例
- JQuery中g(shù)etJSON的使用方法
- JQuery中的$.getJSON 使用說明
- ie下jquery.getJSON的緩存問題的處理方法
- jQuery中$.ajax()和$.getJson()同步處理詳解
- 用原生JavaScript實現(xiàn)jQuery的$.getJSON的解決方法
- jQuery中$.get、$.post、$.getJSON和$.ajax的用法詳解
- jQuery使用getJSON方法獲取json數(shù)據(jù)完整示例
相關(guān)文章
40個有創(chuàng)意的jQuery圖片和內(nèi)容滑動及彈出插件收藏集之三
在網(wǎng)頁的首頁或圖片專題頁面很多地方都會用到圖片滑動插件來循環(huán)切換多張圖片,并且用戶可以點擊左右按鈕來切換圖片。2012-01-01
jquery實現(xiàn)點擊彈出可放大居中及關(guān)閉的對話框(附demo源碼下載)
這篇文章主要介紹了jquery實現(xiàn)點擊彈出可放大居中及關(guān)閉的對話框,具有可拖動、放大、居中及關(guān)閉等功能,提供了2種對話框模式供讀者選擇,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-05-05
ASP.NET jQuery 實例1(在TextBox里面創(chuàng)建一個默認(rèn)提示)
通常用戶在搜索內(nèi)容時,在文本框輸入內(nèi)容前,文本框都會給出默認(rèn)提示,提示用戶輸入正確的內(nèi)容進行搜索2012-01-01
jquery中map函數(shù)與each函數(shù)的區(qū)別實例介紹
​jquery中的each函數(shù)和map函數(shù)的用法看起來差不多,但其實還是有一點區(qū)別的,each返回的是原來的數(shù)組,并不會新創(chuàng)建一個數(shù)組。而map方法會返回一個新的數(shù)組2014-06-06
給artDialog 5.02 增加ajax get功能詳細(xì)介紹
本文將詳細(xì)介紹給artDialog 5.02 增加ajax get功能的方法,按興趣的朋友可以參考2012-11-11
jquery,js簡單實現(xiàn)類似Angular.js雙向綁定
本文主要介紹了jquery,js簡單實現(xiàn)類似Angular.js雙向綁定的方法。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01

