Ajax

Login.html

<head>
    <title>登录</title>
    <mce:script src="js/jquery-1.5.2.js" mce_src="js/jquery-1.5.2.js" type="text/javascript"></mce:script>
    <mce:script src="js/login.js" mce_src="js/login.js" type="text/javascript"></mce:script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="background">
            <div id="loginBox">
                <span id="title">登录</span>
                <div id="LoginMessage">
                    用户名:
                    <input id="txtUser" type="text" maxlength="25" class="txtbox" />
                    <br />
                    密 码:
                    <input id="txtPassword" type="password" maxlength="25" class="txtbox" />
                    <br />
                    <center>
                        <div id="loading">
                            <img src="images/loading.gif" mce_src="images/loading.gif" alt="login"/></div>
                        <input id="btnLogin" type="image" src="images/login.gif" mce_src="images/login.gif"/>
                    </center>
                </div>
            </div>
        </div>
    </div>
    </form>
</body>

  

Login.js

/*----------------------------------------------------------------
// File Name:login.js
// File Introduce
//   check and handler user input
//          
// Create Mark
//   Create Date: 4/20/2011 11:15:19 PM
//   Create by Ben.Jiang
//----------------------------------------------------------------*/

var email_str = /^(?:[a-z/d]+[_/-/+/.]?)*[a-z/d]+@(?:([a-z/d]+/-?)*[a-z/d]+/.)+([a-z]{2,})+$/i; //Email regular expression

$(document).ready(function() {
    $("#btnLogin").click(function() {
        var username = $("#txtUser").val();
        var password = $("#txtPassword").val();
        if (username == "" || password == "") {//check if the input is legal
            alert("用户名和密码不可以为空!");
            return false;
        }
        else if (!email_str.test(username)) {//check if email is legal
            alert("邮件地址格式不正确!");
            return false;
        } else {
            $.ajax({
                type: "POST",
                url: "/Ajax/LoginHandler.ashx", //event handler url
                data: "username=" + escape($('#txtUser').val()) + "&password=" + escape($('#txtPassword').val()),//发送ajax请求
                beforeSend: function() {
                    $("#loading").css("display", "block"); //show loading
                    $("#btnLogin").css("display", "none"); //hide login button
                },
                success: function(msg) {
                    $("#loading").hide(); //hide loading
                    if (msg == "unregistered") {
                        alert("对不起,该用户未注册!");//user is unregistered
                    }
                    if (msg == "frozen") {
                        alert("对不起,该用户已被冻结!");//user id frozen
                    }
                    if (msg == "fail") {
                        alert("对不起,用户名或密码错误!"); //login failed
                    }
                    if (msg == "success") {
                        parent.document.location.href = "manage.aspx"; //login successfully
                    }
                },
                complete: function(data) {
                    $("#loading").css("display", "none"); //hide loading
                    $("#btnLogin").css("display", "block"); //show login
                }
            });
        }
        return false; //stop client continue submit
    }
);
});

  

LoginHandler.ashx:

/*----------------------------------------------------------------
// File Name:LoginHandler.ashx.cs
// File Introduce
//   handler user login
//          
// Create Mark
//   Create Date: 4/20/2011 12:15:19 PM
//   Create by Ben.Jiang
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using PLA.BLL;
using PLA.Model;
using Web.App_Code;

namespace Web.Ajax
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class LoginHandler : IHttpHandler,IRequiresSessionState 
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string username = context.Request["username"];
            string password = context.Request["password"];

            UserBLL userBLL = new UserBLL();
            //check if the user is registered
            if (!userBLL.GetUserRegister(username))
            {
                context.Response.Write("unregistered");
            }//check if the user is frozen
            else if (!userBLL.CheckUserBanStatusByEmail(username))
            {
                context.Response.Write("frozen");
            }
            else
            {
                LoginBLL loginBLL = new LoginBLL();
                //check if the username and password is right
                bool flag = loginBLL.ValidateLogin(username, MD5Helper.getMd5Hash(password), null);
                if (flag)
                {
                    UserInfo user = userBLL.GetUserInfoByEmail(username);
                    context.Session["UID"] = user.U_ID;
                    context.Session["Email"] = user.U_Email;
                    context.Response.Write("success");
                }
                else
                {
                    context.Response.Write("fail");
                }
            }
        }

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

  

原文地址:https://www.cnblogs.com/KTblog/p/4757717.html