`
yangbinfc
  • 浏览: 46334 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jquery 实现ajax

 
阅读更多

  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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics