jQuery getJSON 处理json数据的代码 - Web前端
作者:98wpeu发布时间:2026-06-15分类:网页前端技术浏览:16
导读:html代码:复制代码代码如下:<scripttype="text/javascript"src="/JS/JQuery-1.4.js"></script&...
html代码:
复制代码 代码如下:
<scripttype="text/javascript" src="/JS/JQuery-1.4.js"></script>
<script type="text/JavaScript">
functionjsonTest1()
{
$.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 ) {
returnJQuery.get(url, data, callback, "json");
},
复制代码 代码如下:
public voidPRocessRequest(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);
}
复制代码 代码如下:
<scripttype="text/javascript" src="/JS/JQuery-1.4.js"></script>
<script type="text/JavaScript">
functionjsonTest1()
{
$.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 ) {
returnJQuery.get(url, data, callback, "json");
},
复制代码 代码如下:
public voidPRocessRequest(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源码分析-03构造jQuery对象-源码结构和核心函数 - Web前端
- 获得所有表单值的JQuery实现代码[IE暂不支持] - Web前端
- 可兼容IE的获取及设置cookie的jquery.cookie函数方法 - Web前端
- 20款非常优秀的 jQuery 工具提示插件 推荐 - Web前端
- jQuery中live方法的重复绑定说明 - Web前端
- jquery win 7透明弹出层效果的简单代码 - Web前端
- jQuery 一个图片切换的插件 - Web前端
- 用JQuery模仿淘宝的图片放大镜显示效果 - Web前端
- 详谈 Jquery Ajax异步处理Json数据. - Web前端
- 基于JQuery的Select选择框的华丽变身 - Web前端
- 网页前端技术排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


