Home >Web Front-end >JS Tutorial >AJAX implementation without refreshing login
This time I will bring you AJAX to implement non-refresh login, and what are the precautions for AJAX to implement non-refresh login. The following is a practical case, let's take a look.
I recently learned how to achieve non-refresh login. The general effect is as follows (the interface is ugly, please ignore it...): Click to log in When the button is pressed, a login window pops up. After entering the correct username and password, click Login and the login window closes and the status changes to the current username.Step 1:
First The pop-up window uses controls in jquery-ui. The first step is to learn how to use it.Open thedevelopment-bundle->demos under the decompressed jquery-UI and find index.html, select the model dialog under dialog, right-click to view the source code, observe how to use the control, and find a key code:
$("#dialog-modal").dialog({height: 140,modal: true} );This is for display. Open the source code in the model message and find the key code for closing: $(this).dialog('close'); With these two lines of code, you can control the display and display of the window. Close and you can proceed to the next step. When using it, you need to copy the css folder and js folder of the jquery-ui development package to the project.
Second step:
Here I will first post the code for the general handler that handles AJAX requests. Although I use it before writing it, it is impossible to list it in detail here step by step. In order to facilitate understanding, I will first post the code for the general handler. Out: 1.IsLogin.ashx, used to determine whether the user is logged in, and returns the user name when logged in. Note here that to use session in a general handler, using System.Web.SessionState must be introduced and implemented IRequiresSessionState interfaceusing 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, used to detect whether the user name and password entered by the user match. If it is correct, it will return yes, if it is wrong, it will return no. For the sake of simplicity, there is no
Connect to the database.
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 is used to control user logout and set the session to be empty.
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; } } } }The general processing program is over. The code of the main interface is posted below:
<%@ 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") { $("#pShowLogin").hide(); $("#pShowLoginOut").show(); $("#spanName").text(strs[1]); } else { $("#pShowLogin").show(); $("#pShowLoginOut").hide(); $("#spanState").text("未登录"); } }); } $(function () { isLogin(); //点击登录弹出登录窗口 $("#btnShowLogin").click(function () { //模态窗口,设定长宽 $("#pLogin").dialog({ height: 160, width: 300, modal: true }); }); //点击取消则关闭弹出框 $("#btnCancel").click(function () { $("#pLogin").dialog('close'); }); //点击登录发送post请求在一般处理程序CheckLogin.ashx中验证登录, //根据回调函数结果判断是否登录成功 $("#btnLogin").click(function () { var userName = $("#txtUserName").val(); var password = $("#txtPwd").val(); $.post("/AJAX/CheckLogin.ashx", { "userName": userName, "password": password }, function (data) { if (data == "ok") { $("#pLogin").dialog('close'); isLogin(); } else { alert("用户名或密码错误"); } }); }); //点击注销发送post请求,在一般处理程序中设置session为null,并调用isLogin函数刷新状态 $("#btnExit").click(function () { $.post("/AJAX/LoginOut.ashx", function () { isLogin(); }); }); }); </script> </head> <body> <form id="form1" runat="server"> <p id="pShowLogin" style="display: none"> <span id="spanState"></span> <input type="button" value="登录" id="btnShowLogin" /> </p> <p id="pShowLoginOut" style="display: none"> <span id="spanName"></span> <input type="button" value="注销" id="btnExit" /> </p> <p id="pLogin" title="登录窗口" style="display: none"> <table style="text-align: left" 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 style="text-align: left"> <input type="button" value="取消" id="btnCancel" /></td> </tr> </table> </p> </form> </body> </html>I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How ajax implements the paging query function of bootstrap modal box
Cookie is lost during Ajax cross-domain access How to solve
The above is the detailed content of AJAX implementation without refreshing login. For more information, please follow other related articles on the PHP Chinese website!