文章詳情頁
ajax實(shí)現(xiàn)簡單登錄頁面
瀏覽:104日期:2022-06-11 16:52:30
本文實(shí)例為大家分享了ajax實(shí)現(xià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)簡單的登錄頁面
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})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
Ajax
相關(guān)文章:
1. 使用FormData進(jìn)行Ajax請求上傳文件的實(shí)例代碼2. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)3. axios和ajax的區(qū)別點(diǎn)總結(jié)4. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能5. Ajax返回值類型與用法實(shí)例分析6. Ajax請求超時(shí)與網(wǎng)絡(luò)異常處理圖文詳解7. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)8. ajax請求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹形下拉框的方法9. 解決ajax的delete、put方法接收不到參數(shù)的問題方法10. 關(guān)于Ajax跨域問題及解決方案詳析
排行榜
