ajax實(shí)現(xiàn)簡(jiǎn)單登錄頁(yè)面
本文實(shí)例為大家分享了ajax實(shí)現(xiàn)簡(jiǎn)單登錄頁(yè)面的具體代碼,供大家參考,具體內(nèi)容如下
一.什么是ajax
Ajax是一種無需重新加載整個(gè)網(wǎng)頁(yè),能夠更新部分網(wǎng)頁(yè)的技術(shù)。
二.ajax的工作原理
Ajax工作原理是一個(gè)頁(yè)面的指定位置可以加載另一個(gè)頁(yè)面所有的輸出內(nèi)容,這樣就實(shí)現(xiàn)了一個(gè)靜態(tài)頁(yè)面也能獲取到數(shù)據(jù)庫(kù)中的返回?cái)?shù)據(jù)信息了。 所以Ajax實(shí)現(xiàn)了一個(gè)靜態(tài)網(wǎng)頁(yè)在不刷新整個(gè)頁(yè)面的情況下與服務(wù)器通信,減少了用戶等待時(shí)間,同時(shí)降低了網(wǎng)絡(luò)流量,增強(qiáng)了客戶體驗(yàn)的友好程度。
三.用ajax實(shí)現(xiàn)簡(jiǎn)單的登錄頁(yè)面
1.ajax_login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄頁(yè)面</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 class="div1"></div>
</div>
</body>
</html>
2.views.py
from django.http import HttpResponse,JsonResponse
def login_ajax(request):
"""ajax登錄頁(yè)面"""
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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
本人ajax留言板的源程序 不錯(cuò)的應(yīng)用js
本人ajax留言板的源程序 不錯(cuò)的應(yīng)用js...2007-09-09
非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例附源碼
這篇文章為大家推薦了一個(gè)非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例,可以在不重載頁(yè)面的情況與 Web 服務(wù)器交換數(shù)據(jù),感興趣的小伙伴們可以參考一下2015-11-11
基于Blod的ajax進(jìn)度條下載實(shí)現(xiàn)示例代碼
本篇文章主要介紹了基于Blod的ajax進(jìn)度條下載實(shí)現(xiàn)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
使用$.get()根據(jù)選項(xiàng)的不同從數(shù)據(jù)庫(kù)異步請(qǐng)求數(shù)據(jù)
本例實(shí)現(xiàn)的是這樣的一個(gè)效果:當(dāng)從select下拉框選擇編程語言時(shí)時(shí),根據(jù)選項(xiàng)的不同,異步請(qǐng)求不同的函數(shù)API描述,需要的朋友可以參考下2014-04-04
Ajax Control Toolkit 34個(gè)服務(wù)器端控件
Ajax Control Toolkit 34個(gè)服務(wù)器端控件,想要學(xué)習(xí)ajax的朋友可以參考下。2009-09-09
AJAX請(qǐng)求隊(duì)列實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了AJAX請(qǐng)求隊(duì)列的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
防止ajax重復(fù)請(qǐng)求的方法(GET和POST)
防止ajax重復(fù)請(qǐng)求的方法(GET和POST) ,需要的朋友可以參考下。2011-10-10

