AJAX 实现用户登录验证

html页面中:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../Js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#msg").css("display","none");
            $("#btnLogin").click(function () {
                userLogin();
            });
        });
        function userLogin() {
            var userName = $("#txtUserName").val();
            var userPwd = $("#txtUserPwd").val();
            if (userName != "" && userPwd != "") {
                $.post("UserLogin.ashx", { "userName": userName, "userPwd": userPwd }, function (data) {
                    var serverData = data.split(':');
                    if (serverData[0] == "ok") {
                        window.location.href = "/UserInfoList.aspx";
                    } else {
                        $("#msg").css("display", "block");
                        $("#msg").text(serverData[1]);
                        $("#txtUserName").val("");
                    }

                });

            } else {
                $("#msg").css("display", "block");
                $("#msg").text("用户名密码不能为空!!");
            }
        }
    </script>
</head>
<body>

    用户名:<input type="text" name="txtName" id="txtUserName" />
    密码:<input type="password" name="txtPWD"  id="txtUserPwd"/><br />
        <input type="button" value="登录" id="btnLogin" />
    <span id="msg" style="font-size:14px;color:red"></span><br />
</body>
</html>
UserLogin.ashx.cs页面中:
using XXX.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace XXX.WebApp
{
    /// <summary>
    /// UserLogin 的摘要说明
    /// </summary>
    public class UserLogin : IHttpHandler,System.Web.SessionState.IRequiresSessionState  //用Session要实现该接口
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string userName = context.Request["userName"];
            string userPwd=context.Request["userPwd"];
            BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
            string msg = string.Empty;
            UserInfo userInfo = null;
            if (UserInfoService.ValidateUserInfo(userName, userPwd, out msg, out userInfo))
            {
                context.Session["userInfo"] = userInfo;
                context.Response.Write("ok:"+msg);
            }
            else
            {
                context.Response.Write("no:" + msg);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页