本文實例為大家分享了ajax實現(xiàn)簡單登錄頁面的具體代碼,供大家參考,具體內(nèi)容如下
【相關(guān)文章推薦:ajax視頻教程】
一.什么是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ò)流量,增強了客戶體驗的友好程度。
三.用ajax實現(xiàn)簡單的登錄頁面
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 () { $('#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'); $('.p1').show().html('用戶名或密碼輸入錯誤') } }) }); }); </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('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})
相關(guān)學習推薦:javascript教程
文章標題:ajax實現(xiàn)簡單登錄頁面詳解
瀏覽地址:http://vcdvsql.cn/article30/chseso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)