.Net之路(九).ashx来实现ajax用户名的验证

 

一般处理程序

 

        在说一般处理程序之前,先来看看什么是aspx。在vs中,使用的大多是aspx页面。aspx页面就

是在接收到一个请求之后,将返回的结果生成一个html页面返回给服务器。当我们有时候需要自己来处理这个结果,而不是直接返回html的时候。怎么做呢?这时候就需要一般处理程序了。一般处理程序就是这样一个只有cs页面,而没有前台的文件。扩展名为ashx

 

实例验证用户名

 

      JS


<script type="text/javascript" >

        //验证用户名是否正确
        function JudgeUserName() {            
            var username = $("#userName").val()
            $.ajax({
                type: "Post",
                url: 'RegisterUserNameVerity.ashx/ProcessRequest?username='+username,
                success: function (result) {
                    if (username == "") {
                        alert("用户名不能为空!");
                        document.getElementById("userName").focus();
                        document.getElementById("userName").selected;

                    } else {
if (result == "True") {
                        alert("已存在,请您重新选择一个用户名!")
                        document.getElementById("userName").focus;
                        document.getElementById("userName").select();
                        }  
                          
                    }             
                },
            });
        }


    Html


<input type="text" onblur="JudgeUserName()" />


    一般处理程序


 

        public void ProcessRequest(HttpContext context)
        {
            //获取从前台传过来
            string userName = context.Server.UrlDecode(context.Request.QueryString["username"]);
            //声明查询用户名是否存在的对象
            userBLL verifyusernamebll = new userBLL();
            //通过其他的函数来判断
            bool flag=verifyusernamebll.Exists(userName);
            if (flag==true)
            {
                context.Response.Write(true);
            }
            else
            {
                context.Response.Write(false);
            }
        }

总结

 

       这就是在前台直接可以不通过刷新来进行对用户名的校验,简单的一个小demon。关于一般处理程序,关键的地方就是在前台如何来需要的值传入到后台。这样在只要在一般处理程序拿到了前台的值后,那么操做起来就简单的多了。这里的值直接放在链接里面传过来的。


总结一下就三点:


1.传值,调用一般处理程序

2.处理,将结果返回给前台

3.前台处理返回的结果


原文地址:https://www.cnblogs.com/guziming/p/4232709.html