轻量级的ashx与ajax

<%@ WebHandler Language="C#" Class="Fsll.AJAX" %>  
using System;
using System.Web;
using System.Web.SessionState;
using System.Reflection;
using System.Data;
using Framework.Common.Extensions;

namespace Fsll
{
    /*----------------------------- Demo ----------------------------- 
    $.getJSON("/RMS/RequirementManage/AJAX.ashx?cmd=TestJson",function(json){
        if(json.error!=null && json.error!=undefined && json.error=="1"){  
            //处理错误
            return;  
        }
        //处理数据
    }); 
     */
    /// <summary>
    /// Author     :  yenange
    /// Description:  专用于处理AJAX请求的一般处理程序
    /// Demo       :  如上, 本文件对应 TestJson() 方法
    /// Remark     :  此类是公用的ajax请求页,为了简洁与效率,每个方法请不要超过 10 行。业务逻辑和取数据,请放在BLL层
    /// Date       :  2012-07-31
    /// </summary>
    public class AJAX : IHttpHandler, IReadOnlySessionState
    {
        public AJAX() {
            //初始化Request等  
            Init();
        }
        
        public void ProcessRequest(HttpContext context)
        {
            #region [ 根据Reqeust["cmd"]来执行相应的方法 ]
            try
            {
                String methodName = Request["cmd"];
                Type type = Type.GetType("Fsll.AJAX");
                object instance = Activator.CreateInstance(type);
                MethodInfo method = type.GetMethod(methodName);

                if (null != method)
                {
                    method.Invoke(instance, null);
                }
            }
            catch (Exception ex)
            {
                outputString = "{\"error\":\"1\",\"message\":\"" + ex.Message + "\"}";
            }
            #endregion

            //输出结果  
            ResponseWriteResult();
        }

        #region [ 执行方法. 1.方法名必须与QueryString中的cmd=xxx对应; 2.必须是public类型; 3.输出的内容必须赋给outputString  ]
        public void TestJson()
        {
            //下面注释去掉即可测试:有异常时的处理  
            //throw new Exception("异常测试的信息!");  
            outputString = "{\"name\":\"你不错呀!\"}";
        }
        #endregion

        #region [ Tools ]
        protected HttpRequest Request;
        protected HttpResponse Response;
        protected HttpServerUtility Server;
        protected HttpSessionState Session;
        protected String ContentType;
        protected static String outputString;   //必须为static  

        private void Init()
        {
            Request = HttpContext.Current.Request;
            Response = HttpContext.Current.Response;
            Server = HttpContext.Current.Server;
            Session = HttpContext.Current.Session;
            ContentType = "application/json";
            if (!String.IsNullOrEmpty(Request["ContentType"]))
            {
                ContentType = Request["ContentType"].Replace("_", "/");
            }
            outputString = "{\"error\":\"1\",\"message\":\"没改变outputString初始值.\"}";
        }

        public bool IsReusable
        {
            get
            {
                return false;   //必须为false  
            }
        }
        /// <summary>  
        /// 输出结果  
        /// </summary>  
        public void ResponseWriteResult()
        {
            Response.ContentType = this.ContentType;
            Response.Write(outputString);
        }
        #endregion
    }
}  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <script src="jquery/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function(){
            $.getJSON("AJAX.ashx?cmd=TestJson",function(json){
                if(json.error!=null && json.error!=undefined && json.error=="1"){
                    alert(json.message);
                    return;
                }
                
                alert( json.name );
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="divTest" >
    
    </div>
    </form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值