jquery 实现ajax

本文介绍了一个使用ASP.NET处理JSON序列化的简单示例,通过C#创建Person对象,并将其序列化为JSON格式发送到客户端。客户端使用jQuery解析JSON数据并显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <%@WebHandlerLanguage= "C#" Class= "Handler" %>
  2. usingSystem;
  3. usingSystem.Web;
  4. usingSystem.Web.Script.Serialization;
  5. usingSystem.Collections.Generic;
  6. public class Handler:IHttpHandler{
  7. public void ProcessRequest(HttpContextcontext){
  8. context.Response.ContentType= "text/plain" ;
  9. Personp1= new Person{Age= "22" ,Name= "tom" };
  10. Personp2= new Person{Age= "23" ,Name= "jim" };
  11. Personp3= new Person{Age= "24" ,Name= "lilei" };
  12. IList<Person>persons= new List<Person>{p1,p2,p3};
  13. JavaScriptSerializerjs= new JavaScriptSerializer();
  14. strings=js.Serialize(persons);
  15. context.Response.Write(s);
  16. }
  17. public class Person
  18. {
  19. public stringName{get;set;}
  20. public stringAge{get;set;}
  21. }
  22. public boolIsReusable{
  23. get{
  24. return false ;
  25. }
  26. }
  27. }

先实例化了三个person对象,然后放到一个集合中,最后把这个集合序列化成字符串流到客户端;

客户端:

  1. <!DOCTYPEhtmlPUBLIC "-//W3C//DTDXHTML1.0Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  3. <htmlxmlns= "http://www.w3.org/1999/xhtml" >
  4. <head>
  5. <title></title>
  6. <scriptsrc= "../myjs/jquery-1.4.2.js" type= "text/javascript" ></script>
  7. <scripttype= "text/javascript" >
  8. $.get( "Handler.ashx" , function (data){
  9. var persons=$.parseJSON(data);
  10. $.each(persons, function (key,person){
  11. alert( "Age:" +person.Age+ "Name:" +person.Name)});
  12. });
  13. </script>
  14. </head>
  15. <body>
  16. </body>
  17. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值