jQuery getJSON 处理json数据的代码 - Web前端
作者:98wpeu发布时间:2026-06-15分类:网页前端技术浏览:2
导读: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+WebService 校验账号是否已被注册的代码 - Web前端
- 下一篇:已经是最后一篇了
相关推荐
- Jquery+WebService 校验账号是否已被注册的代码 - Web前端
- jQuery(1.6.3) 中css方法对浮动的实现缺陷分析 - Web前端
- jQueryPad 实用的jQuery测试工具(支持IE,chrome,FF) - Web前端
- JQuery在光标位置插入内容的实现代码 - Web前端
- 锋利的jQuery 要点归纳(二) jQuery中的DOM操作(下) - Web前端
- Jquery调用webService远程访问出错的解决方法 - Web前端
- jquery 打开窗口返回值实现代码 - Web前端
- 使用jquery hover事件实现表格的隔行换色功能示例 - Web前端
- 用jQuery简化Ajax开发实现方法第1/2页 - 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属性


