JS+AJAX(.ASHX) 实现用户登录

html代码:

   <div class="loginbox">
    <ul>
    <li><input id="gonghao" type="text" class="loginuser" value="21022105"/></li> <!--onclick="JavaScript:this.value=''"-->
    <li><input id="mima" type="text" class="loginpwd" value="111111" /></li>       
    <li>
        <input name="" type="button" class="loginbtn" value="" onclick="httpRequest()" />
        <label><input name="" type="checkbox" value="" checked="checked" />记住密码</label>
        <label><a href="#">忘记密码?</a></label></li>
    </ul>    
    </div>

js代码块:

    <script language="javascript" type="text/javascript">
        function httpRequest() {                     
            var gonghao = document.getElementById("gonghao").value;
            var mima = document.getElementById("mima").value;
            document.cookie = "gonghao=" + gonghao;
            document.cookie = "mima=" + mima;
            var xmlHttp;
            if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            } else {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    if (xmlHttp.responseText == "true") {
                        window.location.assign("../main.html");
                    } else {
                        alert('登录失败,用户名或密码不正确!');
                    }
                }
            }            
            xmlHttp.open("get", "LoginHandler.ashx", true);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//application/x-www-form-urlencoded text/html; charset=BIG-5
            xmlHttp.send();
        }
    </script>

LoginHandler.ashx文件代码:

 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.Cookies["gonghao"] != null&& context.Request.Cookies["gonghao"].Value!="") {
                if (context.Request.Cookies["mima"] != null && context.Request.Cookies["mima"].Value != "")
                {
                    string gonghao = context.Request.Cookies["gonghao"].Value;
                    string mima = context.Request.Cookies["mima"].Value;
                    //DataTools.SourceName = "E:\\DB\\users.accdb";
                    string sql = string.Format("select * from Users where 工號='{0}' and 密碼='{1}'", gonghao, mima);
                    DataTable dt= DataTools.AllData(sql);
                    if (dt.Rows.Count > 0) {                        
                        string xingming = dt.Rows[0]["姓名"].ToString();
                        HttpCookie cookie = new HttpCookie("xingming", xingming);
                        context.Response.Cookies.Add(cookie);
                        context.Response.Write("true");
                    }
                }
            }            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值