ajax配合一般處理程序(.ashx)登錄的一般寫法


前端:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>首頁</title>
    <script type="text/javascript" src="JQuery/jquery.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="text" id="txtlogin" value="車輛管理員1" />
            <input type="button" onclick="Login()" value="登錄" />
        </div>
    </form>
    <script type="text/javascript">
        function Login() {
            //var userid = $("#txtlogin").val();
            $.ajax({
                type: "post",
                url: "CarManager/ashx/User.ashx",
                data: { "action": "userlogin", "username": $("#txtlogin").val() },
                dataType: "json",
                success: function (data) {
                    if (data.msg="1") {
                        location.href = "CarManager/Main.aspx";
                    }
                }
            });
        }
    </script>
</body>
</html>

 后端:

public class User : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        StringBuilder _strContent = new StringBuilder();
        if (_strContent.Length == 0)
        {
            string _strAction = context.Request.Params["action"];
            if (string.IsNullOrEmpty(_strAction))
            {
                _strContent.Append("{\"msg\": \"0\", \"msgbox\": \"禁止訪問!\",\"rows\": []}");
            }
            else
            {
                switch (_strAction.Trim().ToLower())
                {
                    case "userlogin": _strContent.Append(UserLogin(context)); break;
                    default: break;
                }
            }
        }
        context.Response.Write(_strContent.ToString());
    }

    private string UserLogin(HttpContext context)
    {
        string result = "";
        string _username = context.Request.Form["username"];
        string _password = context.Request.Form["password"];
        Model.cmUser model = new Model.cmUser();
        if (context.Session["UserModel"] != null)
        {//當前瀏覽器已經有用戶登錄 判斷是不是當前輸入的用戶
            model = (Model.cmUser)context.Session["UserModel"];
            if (model.Name != _username)
            {
                result = "{\"msg\": \"0\", \"msgbox\": \"此瀏覽器已經有其他用戶登錄!\"}";
            }
            else
            {
                result = "{\"msg\": \"1\", \"msgbox\": \"登錄成功!\"}";
            }
        }
        else
        {
            BLL.cmUser bll = new BLL.cmUser();
            string strWhere = string.Format("[Name]='{0}'", _username);// and [Password]='{1}', _password
            DataTable dt = bll.GetList(1, strWhere, " ID ").Tables[0];
            if (dt != null)
            {//用戶和密碼正確
                int _userid = 0;
                int.TryParse(dt.Rows[0]["ID"].ToString(), out _userid);
                model.ID = _userid;
                model.Name = dt.Rows[0]["Name"].ToString();
                int _type = 0;
                int.TryParse(dt.Rows[0]["Type"].ToString(), out _type);
                model.Type = _type;

                context.Session["UserModel"] = model;
                result = "{\"msg\": \"1\", \"msgbox\": \"登錄成功!\"}";
            }
            else
            {
                result= "{\"msg\": \"0\", \"msgbox\": \"用戶名或密碼錯誤!\"}"; ;
            }
        }
        return result;
    }

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM