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;
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章