Home >Web Front-end >JS Tutorial >Detailed explanation of ajax implementation of simple login page

Detailed explanation of ajax implementation of simple login page

coldplay.xixi
coldplay.xixiforward
2020-08-18 17:08:242400browse

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 () {

   $(&#39;#register&#39;).click(function () {
    // alert(&#39;ok&#39;);
    //获取用户名和密码:
    username = $(&#39;#username&#39;).val();
    password = $(&#39;#password&#39;).val();
    rember = $(&#39;#rember&#39;).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(&#39;username&#39;)
      location.href="/index" rel="external nofollow" 

     }else{
      // alert(&#39;username&#39;);
      $(&#39;.p1&#39;).show().html(&#39;用户名或密码输入错误&#39;)

     }
    })
   });
  });
 </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(&#39;username&#39;) # 通过&#39;username&#39;这个键拿到数据
 password = request.POST.get(&#39;password&#39;)


 #若登录正确
 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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete