先來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就死了……以前都是關機重啟就好了,這次關了幾次還是這樣
又是這個謎之BUG……Ajax提交表單,提交前驗證了數據,提交到servlet后就為Null了
Bangk
2016-11-01 14:34:22