jQuery+ajax+asp.net获取Json值的方法
本文实例讲述了jQuery+ajax+asp.net获取Json值的方法。分享给大家供大家参考,具体如下:
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQueryAjaxJson取值示例</title>
<scripttype="text/javascript"src="Scripts/jquery-1.4.4.min.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$("#Button1").click(function(){
$.ajax({
url:'AjaxQuery.aspx',
type:'GET',
dataType:'json',
timeout:1000,
cache:false,
beforeSend:LoadFunction,//加载执行方法
error:erryFunction,//错误执行方法
success:succFunction//成功执行方法
})
functionLoadFunction(){
$("#ddd").html('加载中...');
}
functionerryFunction(){
alert("error");
}
functionsuccFunction(tt){
$("#ddd").html('');
varjson=eval(tt);//数组
$.each(json,function(index,item){
//循环获取数据
varname=json[index].Name;
varage=json[index].Age;
varsex=json[index].Sex;
$("#ddd").html($("#ddd").html()+"<br>"+name+"-"+age+"-"+sex+"<br/>");
});
}
});
})
</script>
</head>
<body>
<inputtype="button"id="Button1"value="获取json数据"/>
<spanid="ddd"></span>
</body>
</html>
//AjaxPostText
functionsavedata(tempid){
vartid=$('#hidtemplate').attr('value');
vardesc=$("#contentdiv").html();
varnum_iid=$("#num_iidArr").attr('value');
varnum_iid2=$("#num_iidArr001").attr('value');//发布页面
vartopsvalue=$("#tops").attr('value');
if(num_iid!=""&&num_iid2!=""){
$.ajax({
url:'TabBaoHandler.ashx',
type:'POST',
data:'type=3&num_iid='+num_iid2+'&tid='+tid+'&desc='+desc+'&top_session='+topsvalue,
dataType:'text',
timeout:20000,
cache:false,
//async:false,//同步
beforeSend:LoadFunction,//加载执行方法
error:erryFunction,//错误执行方法
success:succFunction//成功执行方法
})
functionLoadFunction(){
showLoad("正在运行中...");
}
functionerryFunction(){
$("#contentdiv").html("<pstyle=\"padding:5px\"><imgsrc=\"images/error.png\"/>sorry,提交失败</p>");
closeLoad();
}
functionsuccFunction(tt){
closeLoad();
$("#contentdiv").show().html(tt);
}
}else{
alert("请选择后再操作");
}
}
usingSystem;
//新增
usingSystem.Web.Script.Serialization;
usingSystem.Collections.Generic;
publicpartialclassAjaxQuery:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
List<Student>list=newList<Student>();
Studentc=newStudent();
c.Name="张三";
c.Age=23;
c.Sex="男";
list.Add(c);
Studentcc=newStudent();
cc.Name="李四";
cc.Age=25;
cc.Sex="男";
list.Add(cc);
Studentccc=newStudent();
ccc.Name="李玲";
ccc.Age=25;
ccc.Sex="女";
list.Add(ccc);
Response.ContentType="application/json";
Response.Write(newJavaScriptSerializer().Serialize(list));////这个很关键,否则error
Response.End();
}
}
publicstructStudent
{
publicstringName;
publicintAge;
publicstringSex;
}
}
PS:这里推荐几款本站的json格式数据在线操作工具供大家免费使用,相信在以后的开发中可以派上用场:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。