Home >Web Front-end >JS Tutorial >Detailed explanation of ajax implementation of simple login page
The example in this article shares the specific code for ajax to implement a simple login page for your reference. The specific content is as follows
[Related article recommendations: ajax video Tutorial】
1. What is ajax
Ajax is a technology that can update part of a web page without reloading the entire web page.
2. The working principle of ajax
The working principle of Ajax is that the specified location of one page can load all the output content of another page, thus realizing a static page You can also get the returned data information from the database. Therefore, Ajax implements a static web page to communicate with the server without refreshing the entire page, reducing user waiting time, reducing network traffic, and enhancing the friendliness of the customer experience.
3. Use ajax to implement a simple login page
1.ajax_login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录页面</title> <style> .p1{ display: none; color: red; } </style> <script src="/static/js/jquery-1.12.4.min.js"></script> <script> $(function () { $('#register').click(function () { // alert('ok'); //获取用户名和密码: username = $('#username').val(); password = $('#password').val(); rember = $('#rember').val(); // alert(rember); $.ajax({ url:"/login_ajax_check", type:"POST", //提交方式 data:{"username":username,"password":password,"rember":rember}, dataType:"json", }).done(function (data) { if (data.res==1){ // alert('username') location.href="/index" rel="external nofollow" }else{ // alert('username'); $('.p1').show().html('用户名或密码输入错误') } }) }); }); </script> </head> <body> <p> 用户名:<input type="text" id="username" ><br/> 记住用户名:<input type="checkbox" id="rember"><br/> 密码<input type="password" id="password"><br/> <input type="submit" value="登录" id="register"> <p class="p1"></p> </p> </body> </html>
2.views.py
from django.http import HttpResponse,JsonResponse def login_ajax(request): """ajax登录页面""" return render(request,"booktest/login_ajax.html") def login_ajax_check(request): """ajax登录校验""" username = request.POST.get('username') # 通过'username'这个键拿到数据 password = request.POST.get('password') #若登录正确 if username == "admin" and password == "12": jsonresponse = JsonResponse({"res":1}) return jsonresponse #登录错误: else: return JsonResponse({"res":0})
Related learning recommendations: js video tutorial
The above is the detailed content of Detailed explanation of ajax implementation of simple login page. For more information, please follow other related articles on the PHP Chinese website!