json+ajax

来源:互联网 发布:淘宝评价图片怎么上传 编辑:程序博客网 时间:2024/06/05 20:52

handler2.ashx

 

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
public class Handler2 : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        IList<Person> persons = new List<Person>();
        Person person = new Person();
        person.Name = "sssss";
        person.Gender = "男";
        persons.Add(person);
        var json = new DataContractJsonSerializer(persons.GetType());
        json.WriteObject(context.Response.OutputStream, persons);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

 

Default.aspx中ajax

        $.ajax({
            url: "Handler2.ashx",
            cache: false,
            type: "get",
            datatype: "text",
            success: function (data, textStatus) {

                alert(data);
                var persons = JSON.parse(data);
                alert(persons[0].Name);
                alert(JSON.stringify(persons));
            }
        });

 

原创粉丝点击