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...):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:
nbsp;html> <meta> <title></title> <link> <script></script> <script></script> <script> //判断是否登录,登录则显示登录名,隐藏登录按钮,显示注销按钮 //否则相反 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>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!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!