亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

又是這個謎之BUG……Ajax提交表單,提交前驗證了數據,提交到servlet后就為Null了

又是這個謎之BUG……Ajax提交表單,提交前驗證了數據,提交到servlet后就為Null了

Bangk 2016-11-01 14:34:22
先來JSP表單代碼<form?class="login-form"?method="post"> ????????<input?type="text"?placeholder="用戶名"?id="loginUserName"?name="loginUserName"/> ????????<input?type="password"?placeholder="密碼"?id="loginUserPass"?name="loginUserPass"/> ????????<div><input?style="width:?100px"?type="text"?placeholder="驗證碼"?name="CodeCoreInputA"?id="CodeCoreInputA"/><img?id="CodeCore"?onclick="loing()"?style="margin-left:?20px;float:?right;cursor:?pointer;"?src="CodeCore_ImageServlet"?width="150px"?height="47px;"></div> ????????<button?id="login">登 錄</button> ????????<p?class="message">還沒有賬戶??<a?href="#">立刻創建</a></p> ??????</form>再是JS代碼和Ajax$(function(){ ?$("#logon").click(function(){ ??check_register(); ?}) ?$("#login").click(function(){ ??var?name=$("#loginUserName").val(); ??var?pass=$("#loginUserPass").val(); ??var?CodeCoreInput=$("#CodeCoreInputA").val(); ??if(name!=""?&&?name!="?"){ ???if?(pass!=""?&&?pass!="?")?{ ????if?(CodeCoreInput!=""?&&?CodeCoreInput!="?")?{ ?????Login(); ????}else{ ?????$("#hello?b").html("驗證碼不能為空"); ?????setTimeout(function(){ ??????$("#hello?b").html("Hello!?Please?Login"); ?????},2000); ?????$("#login_form").removeClass('shake_effect');?? ?????setTimeout(function() ???????{ ??????$("#login_form").addClass('shake_effect') ???????},1);? ????} ???}else{ ????$("#hello?b").html("密碼不能為空"); ????setTimeout(function(){ ?????$("#hello?b").html("Hello!?Please?Login"); ????},2000); ????$("#login_form").removeClass('shake_effect');?? ????setTimeout(function() ??????{ ?????$("#login_form").addClass('shake_effect') ??????},1);?? ???} ??} ??else ??{ ???$("#hello?b").html("用戶名不能為空"); ???setTimeout(function(){ ????$("#hello?b").html("Hello!?Please?Login"); ???},2000); ???$("#login_form").removeClass('shake_effect');?? ???setTimeout(function() ?????{ ????$("#login_form").addClass('shake_effect') ?????},1); ??} ?}) ?$('.message?a').click(function?()?{ ??$('form').animate({ ???height:?'toggle', ???opacity:?'toggle' ??},?'slow'); ?}); })最后是servlet代碼public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response) ???throws?ServletException,?IOException?{ ??PrintWriter?out?=?response.getWriter(); ??Object?A?=?request.getParameter("loginUserName"); ??Object?B?=?request.getParameter("loginUserPass"); ??Object?C?=?request.getParameter("CodeCoreInputA"); ??HttpSession?session?=?request.getSession(); ??Object?Code?=?session.getAttribute("Code"); ??if?(C!=Code)?{ ???out.print("Code?Over"); ??}else{ ???User_Logic?UL?=?new?User_Logic(); ???String?val?=?UL.login(A,?B); ???out.print(val); ??} ?} ? ? ?function?Login()?{ ?alert($(".login-form").serialize()); ?$.ajax({ ??type?:?"POST", ??url?:?"Core_UserLogin", ??data:?$(".login-form").serialize(), ??success?:?function(data)?{ ???if(trim(data)=="DBA?Over"){ ????$("#hello?b").html("數據庫異常"); ????setTimeout(function(){ ?????$("#hello?b").html("Hello!?Please?Login"); ????},1500); ????$("#CodeCoreInputA").val(""); ????loing(); ???}else?if(trim(data)=="Account?OR?PassWord?Over"){ ????$("#hello?b").html("用戶名或密碼錯誤"); ????setTimeout(function(){ ?????$("#hello?b").html("Hello!?Please?Login"); ????},1500); ????$("#loginUserName").val(""); ????$("#loginUserPass").val(""); ????$("#CodeCoreInputA").val(""); ????loing(); ???}else?if(trim(data)=="Code?Over"){ ????$("#hello?b").html("驗證碼錯誤"); ????setTimeout(function(){ ?????$("#hello?b").html("Hello!?Please?Login"); ????},1500); ????$("#CodeCoreInputA").val(""); ????loing(); ???}else?if(trim(data)=="PassIsNull"){ ????$("#hello?b").html("歡迎回來"+data); ????setTimeout(function(){ ?????$("#hello?b").html("正在跳轉"); ????},1500); ???} ??} ?}); ? }取值全部為空,使用GET可用成功,但只要用到POST就死了……以前都是關機重啟就好了,這次關了幾次還是這樣
查看完整描述

3 回答

已采納
?
李曉健

TA貢獻1036條經驗 獲得超461個贊

代碼寫的真是夠啰嗦的。。。。。。。。

看著是沒有什么問題,我覺得問題可能是出在那個 button上,因為你在form里只有一個button(當form里只有一個button時,且type不是reset時,這個button會默認為type是submit), ? 所以當你點擊button時就會默認提交form,你的form又沒有寫action,所以默認就是#,當form的action='#'時,提交就相當于提交到當前頁面,也就是相當于刷新頁面。所以就是你的ajax請求還沒有發出去,頁面就被刷新掉了。你可以試試把 ?

<button?id="login">登 錄</button> 換成 ?<a href="javascript:;" id="login">登 錄</a>

查看完整回答
2 反對 回復 2016-11-01
?
Bangk

TA貢獻34條經驗 獲得超21個贊

補充Ajax提交代碼 上面忘記了

function?Login()?{
?alert($(".login-form").serialize());
?$.ajax({
??type?:?"POST",
??url?:?"Core_UserLogin",
??data:?$(".login-form").serialize(),
??success?:?function(data)?{
???if(trim(data)=="DBA?Over"){
????$("#hello?b").html("數據庫異常");
????setTimeout(function(){
?????$("#hello?b").html("Hello!?Please?Login");
????},1500);
????$("#CodeCoreInputA").val("");
????loing();
???}else?if(trim(data)=="Account?OR?PassWord?Over"){
????$("#hello?b").html("用戶名或密碼錯誤");
????setTimeout(function(){
?????$("#hello?b").html("Hello!?Please?Login");
????},1500);
????$("#loginUserName").val("");
????$("#loginUserPass").val("");
????$("#CodeCoreInputA").val("");
????loing();
???}else?if(trim(data)=="Code?Over"){
????$("#hello?b").html("驗證碼錯誤");
????setTimeout(function(){
?????$("#hello?b").html("Hello!?Please?Login");
????},1500);
????$("#CodeCoreInputA").val("");
????loing();
???}else?if(trim(data)=="PassIsNull"){
????$("#hello?b").html("歡迎回來"+data);
????setTimeout(function(){
?????$("#hello?b").html("正在跳轉");
????},1500);
???}
??}
?});
?
}


查看完整回答
反對 回復 2016-11-01
  • 3 回答
  • 3 關注
  • 3248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號