jQuery getJSON 处理json数据的代码

2019-12-24,,,

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这样实现的:
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数据$.getJSON方法的实例代码
  • Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
  • jquery $.getJSON()跨域请求
  • Jquery getJSON方法详细分析
  • jQuery+ajax中getJSON() 用法实例
  • JQuery中getJSON的使用方法
  • JQuery中的$.getJSON 使用说明
  • ie下jquery.getJSON的缓存问题的处理方法
  • jQuery中$.ajax()和$.getJson()同步处理详解
  • 用原生JavaScript实现jQuery的$.getJSON的解决方法
  • jQuery中$.get、$.post、$.getJSON和$.ajax的用法详解
  • jQuery使用getJSON方法获取json数据完整示例

《jQuery getJSON 处理json数据的代码.doc》

下载本文的Word格式文档,以方便收藏与打印。