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" οnclick="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;
        }
    }
}

 

转载于:https://www.cnblogs.com/zhyue93/p/ajax_login.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值