JS+AJAX 实现用户登录

来源:互联网 发布:淘宝天猫卖家信息采集 编辑:程序博客网 时间:2024/05/09 05:55

 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 0
原创粉丝点击