JQuery Ajax JSon实例

---Demo.aspx 页面



    
    
    


    
工号:        姓名:            年龄:      角色:              

---代码处理程序Handler1.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq; //下载Newtonsoft.Json.dll,添加引用

namespace WebApplication
{
    /// 
    /// Handler1 的摘要说明
    /// 
    public class Handler1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Dictionary returnValue = new Dictionary();
            JObject json = JObject.Parse(context.Request["SEND_JSON"]);
            try
            {
                switch (json["type"].ToString())
                {
                    case "QueryUser":
                        returnValue = QueryUser(json);
                        break;
                }
            }
            catch (Exception er)
            {
                returnValue.Add("error", er.Message);
            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            context.Response.ContentType = "text/plain";
            context.Response.Write(jss.Serialize(returnValue));
        }

        private Dictionary QueryUser(JObject json)
        {
            //下面一句代码抛出错误,检测Demo.aspx页面错误处理代码
            //throw new Exception("执行报错!");

            Dictionary dict = null;
            string username = json["userName"].ToString();
            string UserID = json["userID"].ToString();
            dict = new Dictionary();
            dict.Add("role", "Admin");
            dict.Add("age", "24");
            return dict;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }



你可能感兴趣的:(ASP.NET)