文章詳情頁
ajax實(shí)現(xiàn)簡(jiǎn)單登錄頁面
瀏覽:111日期:2022-06-11 16:52:30
本文實(shí)例為大家分享了ajax實(shí)現(xiàn)簡(jiǎn)單登錄頁面的具體代碼,供大家參考,具體內(nèi)容如下
一.什么是ajax
Ajax是一種無需重新加載整個(gè)網(wǎng)頁,能夠更新部分網(wǎng)頁的技術(shù)。
二.ajax的工作原理
Ajax工作原理是一個(gè)頁面的指定位置可以加載另一個(gè)頁面所有的輸出內(nèi)容,這樣就實(shí)現(xiàn)了一個(gè)靜態(tài)頁面也能獲取到數(shù)據(jù)庫中的返回?cái)?shù)據(jù)信息了。 所以Ajax實(shí)現(xiàn)了一個(gè)靜態(tài)網(wǎng)頁在不刷新整個(gè)頁面的情況下與服務(wù)器通信,減少了用戶等待時(shí)間,同時(shí)降低了網(wǎng)絡(luò)流量,增強(qiáng)了客戶體驗(yàn)的友好程度。
三.用ajax實(shí)現(xiàn)簡(jiǎ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("用戶名或密碼輸入錯(cuò)誤") } }) }); }); </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登錄校驗(yàn)""" username = request.POST.get("username") # 通過"username"這個(gè)鍵拿到數(shù)據(jù) password = request.POST.get("password") #若登錄正確 if username == "admin" and password == "12": jsonresponse = JsonResponse({"res":1}) return jsonresponse #登錄錯(cuò)誤: else: return JsonResponse({"res":0})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
Ajax
相關(guān)文章:
1. ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)2. 解決ajax的delete、put方法接收不到參數(shù)的問題方法3. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法4. axios和ajax的區(qū)別點(diǎn)總結(jié)5. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)6. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題7. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)8. 爬取今日頭條Ajax請(qǐng)求9. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算10. layui Ajax請(qǐng)求給下拉框賦值的實(shí)例
排行榜
