溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ajax如何實現(xiàn)帶有驗證碼的局部刷新登錄界面

發(fā)布時間:2021-05-18 14:28:09 來源:億速云 閱讀:225 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)Ajax如何實現(xiàn)帶有驗證碼的局部刷新登錄界面的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

運行界面:

Ajax如何實現(xiàn)帶有驗證碼的局部刷新登錄界面

1.驗證碼后臺訪問部分上一篇博客已經(jīng)介紹多了,這里介紹如何利用img中src實現(xiàn)局部刷新驗證碼的功能。

html部分:

<p><label class="lbright">驗證碼:</label>
<span>
<input type="text" name="validcode"  id="validcode"/>
<img id="codePic" src="http://127.0.0.1:8888/TP/codePic" width="60" height="21" /> 
</span> 
<a class="blurry" id="newPic" onclick="getPic();">看不清楚,換一張</a>
</p>

js部分:

<script type="text/javascript">
function getPic(){ 
$("#codePic").attr("src","http://127.0.0.1:8888/TP/codePic?flag="+Math.random()); 
};
</script>

這部分最重要的就是 $("#codePic").attr("src","http://127.0.0.1:8888/TP/codePic?flag="+Math.random()); 這部分的代碼。如果不加flag="+Math.random()是實現(xiàn)不了局部刷新的功能的。因為src中如果每次訪問的地址一樣的話就會發(fā)生不更新的情況。具體為什么會發(fā)生這種情況大家可以自己去研究。而codePic其實是一個action。這個action的功能是利用java畫筆畫出驗證碼并打包成圖片返回給img中的src。

2.利用bootstrap中的modal實現(xiàn)對話框的功能。因為登錄提交前需要驗證用戶名或密碼是否為空等判斷,如果出現(xiàn)錯誤就需要彈出對話框提示用戶。這里驗證部分用js實現(xiàn),對話框部分用bootstrap的modal實現(xiàn)。

html對話框部分:

<div class="modal" id="mymodal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h5 class="modal-title">親,您好</h5>
</div>
<div class="modal-body" id="dialogs">
<p></p>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" style="color: #FFFFFF;background-color:#FB8F02; text-align:center;
padding:10px;border: 1px solid #dedede;-moz-border-radius: 15px;-webkit-border-radius: 15px; border-radius:15px;vertical-align:middle;">我知道了
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

js驗證部分:

<script type="text/javascript">
function dialog(){ 
$("#mymodal").modal("toggle");
};
function login(){ 
var userName=document.getElementById("username").value; 
var pwd=document.getElementById("password").value; 
var validcode=document.getElementById("validcode").value; 
var matchResult=true; 
if(userName==""){ 
document.getElementById("dialogs").innerHTML="<h4>用戶賬號不能為空!</h4>";
dialog();
matchResult=false; 
}else if(pwd==""){ 
document.getElementById("dialogs").innerHTML="<h4>用戶密碼不能為空!</h4>";
dialog();
matchResult=false; 
}else if(validcode==""){ 
document.getElementById("dialogs").innerHTML="<h4>驗證碼不能為空!</h4>";
dialog();
matchResult=false; 
}else if(userName.length<6||userName.length>20){ 
document.getElementById("dialogs").innerHTML="<h4>用戶名長度應(yīng)在6到20個字符之間!</h4>";
dialog();
matchResult=false; 
}else if(pwd.length<6||pwd.length>20){ 
document.getElementById("dialogs").innerHTML="<h4>密碼或重復密碼長度應(yīng)在6到20個字符之間!</h4>";
dialog();
matchResult=false; 
} 
};
</script>

需要導入的css、js文件:

<link href="css/global.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src='js/jquery-1.9.1.js'></script>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>

這里需要注意的就是導入的CSS,js文件是否正確。

3.利用ajax實現(xiàn)登錄功能

html部分:

<div class="submitcon">
<input type="button" value="登 錄" style="height:45px;width:130px;margin-top:15px;color: #FFFFFF;background-color:#FB8F02;font-size: 20px;
padding:5px;border: 3px solid #dedede;-moz-border-radius: 15px;-webkit-border-radius: 15px; border-radius:15px;vertical-align:middle;text-align:center;" onclick="login();"/> 
</div>

js部分:

<script type="text/javascript">
function login(){ 
var userName=document.getElementById("username").value; 
var pwd=document.getElementById("password").value; 
var validcode=document.getElementById("validcode").value; 
var matchResult=true; 
if(userName==""){ 
document.getElementById("dialogs").innerHTML="<h4>用戶賬號不能為空!</h4>";
dialog();
matchResult=false; 
}else if(pwd==""){ 
document.getElementById("dialogs").innerHTML="<h4>用戶密碼不能為空!</h4>";
dialog();
matchResult=false; 
}else if(validcode==""){ 
document.getElementById("dialogs").innerHTML="<h4>驗證碼不能為空!</h4>";
dialog();
matchResult=false; 
}else if(userName.length<6||userName.length>20){ 
document.getElementById("dialogs").innerHTML="<h4>用戶名長度應(yīng)在6到20個字符之間!</h4>";
dialog();
matchResult=false; 
}else if(pwd.length<6||pwd.length>20){ 
document.getElementById("dialogs").innerHTML="<h4>密碼或重復密碼長度應(yīng)在6到20個字符之間!</h4>";
dialog();
matchResult=false; 
} 
if(matchResult==true){
$.post("http://127.0.0.1:8888/TP/usersAction?method=login", {usersName:userName,password:pwd, validcode:validcode},function(data,status){
var error=data.error;
var result=data.result; 
getPic();
if(error=="error"){
errors="true";
document.getElementById("dialogs").innerHTML="<h4>驗證碼錯誤,請重新輸入!</h4>";
dialog();
}
if(result=="0"){
document.getElementById("dialogs").innerHTML="<h4>您輸入的用戶名不存在!</h4>";
document.getElementById("username").value="";
dialog();
}else if(result=="1"){
document.getElementById("dialogs").innerHTML="<h4>您輸入的密碼錯誤,請重新輸入!</h4>";
document.getElementById("password").value="";
dialog();
}else if(result=="2"){
document.getElementById("dialogs").innerHTML="<h4>您的管理員權(quán)限不夠!</h4>";
dialog();
}else if(result=="3"){
location.href="http://127.0.0.1:8888/TP/main.jsp";
} 
},"json");
} 
};
</script>

這里location.href="http://127.0.0.1:8888/TP/main.jsp"作用相當于重定向。我的ajax不是原生的js中的ajax而是JQuery封裝好的ajax。大家可以去搜一搜 JQuery中$.post()請求。

login.jsp全部代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>無標題文檔</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/global.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src='js/jquery-1.9.1.js'></script>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="modal" id="mymodal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h5 class="modal-title">親,您好</h5>
</div>
<div class="modal-body" id="dialogs">
<p></p>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" style="color: #FFFFFF;background-color:#FB8F02; text-align:center;
padding:10px;border: 1px solid #dedede;-moz-border-radius: 15px;-webkit-border-radius: 15px; border-radius:15px;vertical-align:middle;">我知道了
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="logincontainer">
<div class="logintitle">大學二手交易平臺管理系統(tǒng)</div>
<div class="loginbg">
<div class="loginfmbg">
<div class="sysname">BBW綜合管理系統(tǒng)</div>
<div class="loginfm"> 
<p><label class="lbright">用戶名:</label>
<span class="spinput"><input type="text" name="usersName" id="username"/></span>
</p>
<p><label class="lbright">密 碼:</label>
<span class="spinput"><input type="password" name="password" id="password"/></span></p>
<p><label class="lbright">驗證碼:</label>
<span>
<input type="text" name="validcode"  id="validcode"/>
<img id="codePic" src="http://127.0.0.1:8888/TP/codePic" width="60" height="21" /> 
</span> 
<a class="blurry" id="newPic" onclick="getPic();">看不清楚,換一張</a>
</p> 
</div>
<div class="submitcon">
<input type="button" value="登 錄" style="height:45px;width:130px;margin-top:15px;color: #FFFFFF;background-color:#FB8F02;font-size: 20px;
padding:5px;border: 3px solid #dedede;-moz-border-radius: 15px;-webkit-border-radius: 15px; border-radius:15px;vertical-align:middle;text-align:center;" onclick="login();"/> 
</div>
</div>
</div>
<div class="copyright">Copyright 2015-2016 林志強 版權(quán)所有 </div>
</div>
</body>
<script type="text/javascript">
function getPic(){ 
$("#codePic").attr("src","http://127.0.0.1:8888/TP/codePic?flag="+Math.random()); 
};
function dialog(){ 
$("#mymodal").modal("toggle");
};
function login(){ 
var userName=document.getElementById("username").value; 
var pwd=document.getElementById("password").value; 
var validcode=document.getElementById("validcode").value; 
var matchResult=true; 
if(userName==""){ 
document.getElementById("dialogs").innerHTML="<h4>用戶賬號不能為空!</h4>";
dialog();
matchResult=false; 
}else if(pwd==""){ 
document.getElementById("dialogs").innerHTML="<h4>用戶密碼不能為空!</h4>";
dialog();
matchResult=false; 
}else if(validcode==""){ 
document.getElementById("dialogs").innerHTML="<h4>驗證碼不能為空!</h4>";
dialog();
matchResult=false; 
}else if(userName.length<6||userName.length>20){ 
document.getElementById("dialogs").innerHTML="<h4>用戶名長度應(yīng)在6到20個字符之間!</h4>";
dialog();
matchResult=false; 
}else if(pwd.length<6||pwd.length>20){ 
document.getElementById("dialogs").innerHTML="<h4>密碼或重復密碼長度應(yīng)在6到20個字符之間!</h4>";
dialog();
matchResult=false; 
} 
if(matchResult==true){
$.post("http://127.0.0.1:8888/TP/usersAction?method=login", {usersName:userName,password:pwd, validcode:validcode},function(data,status){
var error=data.error;
var result=data.result; 
getPic();
if(error=="error"){
errors="true";
document.getElementById("dialogs").innerHTML="<h4>驗證碼錯誤,請重新輸入!</h4>";
dialog();
}
if(result=="0"){
document.getElementById("dialogs").innerHTML="<h4>您輸入的用戶名不存在!</h4>";
document.getElementById("username").value="";
dialog();
}else if(result=="1"){
document.getElementById("dialogs").innerHTML="<h4>您輸入的密碼錯誤,請重新輸入!</h4>";
document.getElementById("password").value="";
dialog();
}else if(result=="2"){
document.getElementById("dialogs").innerHTML="<h4>您的管理員權(quán)限不夠!</h4>";
dialog();
}else if(result=="3"){
location.href="http://127.0.0.1:8888/TP/main.jsp";
} 
},"json");
} 
};
</script>
</html>

什么是ajax

ajax是一種在無需重新加載整個網(wǎng)頁的情況下,能夠更新部分網(wǎng)頁的技術(shù),可以通過在后臺與服務(wù)器進行少量數(shù)據(jù)交換,使網(wǎng)頁實現(xiàn)異步更新。

感謝各位的閱讀!關(guān)于“Ajax如何實現(xiàn)帶有驗證碼的局部刷新登錄界面”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI