jQuerygetJSON处理json数据的代码_jquery
来源:动视网
责编:小采
时间:2020-11-27 20:54:15
jQuerygetJSON处理json数据的代码_jquery
jQuerygetJSON处理json数据的代码_jquery:Html代码: 代码如下: function jsonTest1() { $.ajax({ url:Handler.ashx, data:{type:ajax}, datatype:json, type:get, success:function(data) { document.getElementById('div1')
导读jQuerygetJSON处理json数据的代码_jquery:Html代码: 代码如下: function jsonTest1() { $.ajax({ url:Handler.ashx, data:{type:ajax}, datatype:json, type:get, success:function(data) { document.getElementById('div1')

Html代码:
代码如下:
script>
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
{
alert(data[i]["UserId"])
}
}
);
}
script>
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 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);
}
jQuerygetJSON处理json数据的代码_jquery
jQuerygetJSON处理json数据的代码_jquery:Html代码: 代码如下: function jsonTest1() { $.ajax({ url:Handler.ashx, data:{type:ajax}, datatype:json, type:get, success:function(data) { document.getElementById('div1')