文章詳情頁(yè)
ajax實(shí)現(xiàn)簡(jiǎn)單登錄頁(yè)面
瀏覽:395日期:2022-06-11 16:52:30
本文實(shí)例為大家分享了ajax實(shí)現(xiàn)簡(jiǎn)單登錄頁(yè)面的具體代碼,供大家參考,具體內(nèi)容如下
一.什么是ajax
Ajax是一種無(wú)需重新加載整個(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></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") # 通過(guò)"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. ajax和fetch的區(qū)別點(diǎn)總結(jié)2. PHP下ajax跨域的解決方案之jsonp實(shí)例分析3. Android制作登錄頁(yè)面并且記住賬號(hào)密碼功能的實(shí)現(xiàn)代碼4. 基于JSON的高級(jí)AJAX開(kāi)發(fā)技術(shù)5. 如何封裝一個(gè)Ajax函數(shù)6. Vue登錄頁(yè)面的動(dòng)態(tài)粒子背景插件實(shí)現(xiàn)7. Ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單8. 使用AJAX異步通信技術(shù)實(shí)現(xiàn)搜索聯(lián)想和自動(dòng)補(bǔ)全示例9. 關(guān)于Ajax跨域問(wèn)題及解決方案詳析10. 使用AJAX實(shí)現(xiàn)上傳文件
排行榜

網(wǎng)公網(wǎng)安備