


How to use AJax and json to implement login verification in php
php uses AJax and json to implement login verification: 1. Create a jsp sample file and import jquery dependency and fastjson dependency files; 2. Create a new login.js file to obtain the user name and password text content; 3. , create a new controller class, query whether the user exists and convert the object into a json string type and return it to the js file; 4. js determines whether it is successful and then jumps to the page.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
AJAX and Json complete user login
1. Import jquery dependencies and fastjson dependencies in advance
2. Create a new jsp page
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-3.4.1.js"></script> <script type="text/javascript" src="login.js"></script> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <!-- 不使用submit,用ajax+json实现局部刷新,实现登录 --> <form action="" method="post"> <span id="msg"></span><br/> 用户姓名:<input type="text" name="username" id="username"><br/> 用户密码:<input type="text" name="password" id="password"><br/> <input type="button" value="登录" id="submit"> </form> </body> </html>
3. Create a new js file
$(function(){ $("#submit").click(function(){ var username = $("#username").val(); var password = $("#password").val(); //获取json格式的文本内容 $.post("login?mark=login",{"username":username,"password":password}, function(data){ if(data.log){ /*输入要跳转的页面*/ /*window.location.href="https://www.php.cn/link/3729ff995bfa947622cdf0612e57c332";*/ alert("success"); }else{ $("#msg").css("color","red").html(data.msg); } },"json" ); }); });
4. Create a new controller class
Query whether This user exists
Convert the map object into a json string type, write it to the memory, and return it to the js file
private void login(HttpServletRequest request, HttpServletResponse response) throws Exception { // response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); String msg = ""; String username = request.getParameter("username"); String password = request.getParameter("password"); Map<String, Object> map = new HashMap(); //查询是否存在此用户 User user = new LoginServer().login(username, password); if(user!=null) { map.put("log", true); map.put("msg", "成功"); }else { map.put("log", false); map.put("msg", "用户名或者密码错误"); } //把map对象转换成json字符串类型,写入到内存,并返回给js文件 writer.write(JSON.toJSONString(map)); }
The above is the detailed content of How to use AJax and json to implement login verification in php. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON
