您好,登錄后才能下訂單哦!
小編給大家分享一下ajax如何實現(xiàn)簡單的登錄頁面,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
一.什么是ajax
Ajax是一種無需重新加載整個網(wǎng)頁,能夠更新部分網(wǎng)頁的技術(shù)。
二.ajax的工作原理
Ajax工作原理是一個頁面的指定位置可以加載另一個頁面所有的輸出內(nèi)容,這樣就實現(xiàn)了一個靜態(tài)頁面也能獲取到數(shù)據(jù)庫中的返回數(shù)據(jù)信息了。 所以Ajax實現(xiàn)了一個靜態(tài)網(wǎng)頁在不刷新整個頁面的情況下與服務(wù)器通信,減少了用戶等待時間,同時降低了網(wǎng)絡(luò)流量,增強(qiáng)了客戶體驗的友好程度。
三.用ajax實現(xiàn)簡單的登錄頁面
1.ajax_login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁面</title> <style> .div1{ 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'); $('.div1').show().html('用戶名或密碼輸入錯誤') } }) }); }); </script> </head> <body> <div> 用戶名:<input type="text" id="username" ><br/> 記住用戶名:<input type="checkbox" id="rember"><br/> 密碼<input type="password" id="password"><br/> <input type="submit" value="登錄" id="register"> <div class="div1"></div> </div> </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'這個鍵拿到數(shù)據(jù) password = request.POST.get('password') #若登錄正確 if username == "admin" and password == "12": jsonresponse = JsonResponse({"res":1}) return jsonresponse #登錄錯誤: else: return JsonResponse({"res":0})
以上是“ajax如何實現(xiàn)簡單的登錄頁面”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。