文章詳情頁
ajax實(shí)現(xiàn)簡單登錄頁面
瀏覽:119日期:2022-06-11 16:52:30
本文實(shí)例為大家分享了ajax實(shí)現(xiàn)簡單登錄頁面的具體代碼,供大家參考,具體內(nèi)容如下
一.什么是ajax
Ajax是一種無需重新加載整個網(wǎng)頁,能夠更新部分網(wǎng)頁的技術(shù)。
二.ajax的工作原理
Ajax工作原理是一個頁面的指定位置可以加載另一個頁面所有的輸出內(nèi)容,這樣就實(shí)現(xiàn)了一個靜態(tài)頁面也能獲取到數(shù)據(jù)庫中的返回數(shù)據(jù)信息了。 所以Ajax實(shí)現(xiàn)了一個靜態(tài)網(wǎng)頁在不刷新整個頁面的情況下與服務(wù)器通信,減少了用戶等待時間,同時降低了網(wǎng)絡(luò)流量,增強(qiáng)了客戶體驗的友好程度。
三.用ajax實(shí)現(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></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})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
Ajax
相關(guān)文章:
1. php判斷一個請求是ajax請求還是普通請求的方法2. AJAX淺析數(shù)據(jù)交換的實(shí)現(xiàn)3. Ajax實(shí)現(xiàn)搜索功能的分頁4. AJAX亂碼與異步同步以及封裝jQuery庫實(shí)現(xiàn)步驟詳解5. Ajax驗證用戶名是否存在的實(shí)例代碼6. 前后端ajax和json數(shù)據(jù)交換方式7. Jquery使用原生AJAX方法請求數(shù)據(jù)8. ajax實(shí)現(xiàn)提交時校驗表單方法9. ThinkPHP5 通過ajax插入圖片并實(shí)時顯示(完整代碼)10. 利用ajax+php實(shí)現(xiàn)商品價格計算
排行榜
