這篇文章主要為大家展示了“AJAX怎么實(shí)現(xiàn)無刷新登錄功能”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“AJAX怎么實(shí)現(xiàn)無刷新登錄功能”這篇文章吧。
創(chuàng)新互聯(lián)是一家專業(yè)提供邵武企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、html5、小程序制作等業(yè)務(wù)。10年已為邵武眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
最近學(xué)習(xí)了如何實(shí)現(xiàn)無刷新登錄,大體的效果如下(界面比較丑,請(qǐng)自行忽略....):
點(diǎn)擊登錄按鈕時(shí)彈出登錄窗口,輸入正確的用戶名密碼后點(diǎn)擊登錄則登錄窗口關(guān)閉,狀態(tài)改為當(dāng)前用戶名.
第一步:
首先彈出窗口使用的是jquery-ui中的控件,第一步要學(xué)會(huì)如何使用.
打開解壓后的jquery-UI下的development-bundle->demos
,找到index.html,選擇dialog下的model dialog,右鍵查看源碼,觀察如何使用該控件,找到一句關(guān)鍵代碼:$("#dialog-modal").dialog({height: 140,modal: true});
這是用于顯示的,打開model message中的源碼,找到關(guān)閉的關(guān)鍵代碼:$(this).dialog('close');有了這兩句代碼,可以控制窗口的顯示與關(guān)閉,可以進(jìn)行下一步了.使用時(shí)需復(fù)制jquery-ui開發(fā)包的css文件夾,js文件夾到項(xiàng)目中.
第二步:
在這里先貼出處理AJAX請(qǐng)求的一般處理程序的代碼,雖然正真寫的時(shí)候都是用到再寫,但這里不可能一步一步詳細(xì)列出,為了便于理解,先將一般處理程序代碼貼出來:
1.IsLogin.ashx,用于判斷用戶是否登錄,登錄則返回用戶名.這里注意,在一般處理程序中要使用session,必須引入using System.Web.SessionState且要實(shí)現(xiàn)IRequiresSessionState接口
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace AJAX無刷新登錄.AJAX { /// <summary> /// IsLogin 的摘要說明 /// </summary> public class IsLogin : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Session["userName"] != null) { string userName = context.Session["userName"].ToString(); context.Response.Write("yes|"+userName); } else { context.Response.Write("no"); } } public bool IsReusable { get { return false; } } } }
2.CheckLogin.ashx,用于檢測(cè)用戶輸入用戶名密碼是否匹配,正確則返回yes,錯(cuò)誤返回no,這里為了簡便沒有連接數(shù)據(jù)庫.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace AJAX無刷新登錄.AJAX { /// <summary> /// CheckLogin 的摘要說明 /// </summary> public class CheckLogin : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string userName = context.Request["userName"]; string password=context.Request["password"]; if (userName=="admin"&&password=="admin") { context.Session["userName"] = "admin"; context.Response.Write("ok"); } else { context.Response.Write("no"); } } public bool IsReusable { get { return false; } } } }
3.LoginOut.ashx,用于控制用戶登出,設(shè)置session為空.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace AJAX無刷新登錄.AJAX { /// <summary> /// LoginOut 的摘要說明 /// </summary> public class LoginOut : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Session["userName"] = null; } public bool IsReusable { get { return false; } } } }
一般處理程序就結(jié)束了,下面貼出主界面的代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="AJAX無刷新登錄.Login" %>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="JQueryUI/css/ui-lightness/jquery-ui-1.8.2.custom.css" rel="stylesheet" /> <script src="JQueryUI/jquery-1.4.2.min.js"></script> <script src="JQueryUI/jquery-ui-1.8.2.custom.min.js"></script> <script type="text/javascript"> //判斷是否登錄,登錄則顯示登錄名,隱藏登錄按鈕,顯示注銷按鈕 //否則相反 var isLogin = function () { $.post("/AJAX/IsLogin.ashx", function (data) { var strs = data.split('|'); if (strs[0] == "yes") { $("#divShowLogin").hide(); $("#divShowLoginOut").show(); $("#spanName").text(strs[1]); } else { $("#divShowLogin").show(); $("#divShowLoginOut").hide(); $("#spanState").text("未登錄"); } }); } $(function () { isLogin(); //點(diǎn)擊登錄彈出登錄窗口 $("#btnShowLogin").click(function () { //模態(tài)窗口,設(shè)定長寬 $("#divLogin").dialog({ height: 160, width: 300, modal: true }); }); //點(diǎn)擊取消則關(guān)閉彈出框 $("#btnCancel").click(function () { $("#divLogin").dialog('close'); }); //點(diǎn)擊登錄發(fā)送post請(qǐng)求在一般處理程序CheckLogin.ashx中驗(yàn)證登錄, //根據(jù)回調(diào)函數(shù)結(jié)果判斷是否登錄成功 $("#btnLogin").click(function () { var userName = $("#txtUserName").val(); var password = $("#txtPwd").val(); $.post("/AJAX/CheckLogin.ashx", { "userName": userName, "password": password }, function (data) { if (data == "ok") { $("#divLogin").dialog('close'); isLogin(); } else { alert("用戶名或密碼錯(cuò)誤"); } }); }); //點(diǎn)擊注銷發(fā)送post請(qǐng)求,在一般處理程序中設(shè)置session為null,并調(diào)用isLogin函數(shù)刷新狀態(tài) $("#btnExit").click(function () { $.post("/AJAX/LoginOut.ashx", function () { isLogin(); }); }); }); </script> </head> <body> <form id="form1" runat="server"> <div id="divShowLogin" > <span id="spanState"></span> <input type="button" value="登錄" id="btnShowLogin" /> </div> <div id="divShowLoginOut" > <span id="spanName"></span> <input type="button" value="注銷" id="btnExit" /> </div> <div id="divLogin" title="登錄窗口" > <table id="tbLoin"> <tr> <td>用戶名:</td> <td> <input type="text" id="txtUserName" /></td> </tr> <tr> <td>密碼:</td> <td> <input type="password" id="txtPwd" /></td> </tr> <tr> <td> <input type="button" value="登錄" id="btnLogin" /></td> <td > <input type="button" value="取消" id="btnCancel" /></td> </tr> </table> </div> </form> </body> </html>
以上是“AJAX怎么實(shí)現(xiàn)無刷新登錄功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:AJAX怎么實(shí)現(xiàn)無刷新登錄功能
網(wǎng)頁鏈接:http://aaarwkj.com/article4/phdsie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)