If Condition在Javascript中总是失败



以下是jsp页面上的代码。。这是一个用户登录页面。。

<form onsubmit="return userValidation()" action="tempTestingPage.jsp" method="post">
<table>
<b><h3 style="text-align: center">Welcome back!</h3></b>
<h5 style="text-align: center">Sign in to continue to your account</h5>
<tr>
<td><label>User Name</label></td>
<td><input autofocus type="text" id="userloginid" name="LoginID" required="Enter your user name here"</td>
</tr>
<tr>
<td><br><label>Password</label></td>
<td><br><input type="text" id="userloginpassword" name="LoginPassword" required="Enter your Password here"</td>
</tr>
</table>
<div style="text-align: center;">
<br><input type="submit" style="width: 33%; border-radius: 10px; border-color: #ea0f1f;" value="Sign in">
</div>
<br><div id="registerlogin" style="text-align: center;">
Not Registered Yet? <a style="text-decoration: none; color: #8a2de3;" href="RegistrationForm.jsp" >Register Here</a>
</div>
</form>
</fieldset>
</div>

我编写代码的方式是,在提交上述表单时,如果javascript函数返回true,它会导航到另一个jsp页面,即tempTestingPage.jsp。否则,通过在javascript函数中显示其可见性,会显示以下错误。下面的所有javascript代码也是在上面的jsp页面上编写的。

<div id="invalidusernameorpassword" style="visibility: hidden">
<fieldset style="position: absolute; left: 37.4%; top: 7%; display: block; background-color: #fae4a8 ;width: 26%; height: 9.3%; border: 4px; border-color: #cd8686; border-style: solid;">
<table>
<tr>
<td><h4 style="font-family: sans-serif; text-align: left; margin-top: 0%; margin-bottom: 0%; color: #f40909; font-weight: bold;">There was a problem with your request.</h4></td>
</tr>
<tr>
<td><p style="font-family: sans-serif; text-align: left; font-size: 74%; margin-top: 0%; color: #f40909;">There was an error with your Username or Password combination. Please try again.</p></td>
</tr>
</table>
</fieldset>
</div>

以下是我的javascript函数userValidation(),它在表单提交时被调用:在这段代码中,我将向servlet UserPermissionServlet发送一个ajax请求,以确定输入的用户名和密码的组合是否正确。如果组合正确,则返回"found"作为响应。否则返回"未找到"。无论响应是什么…if条件总是失败,else部分的语句都会被执行。代码中放置了警报,仅供我参考。

function userValidation() {
var result = ajaxForPermissionInside("UserPermissionServlet?data1=" + document.getElementById("userloginid").value + "&data2=" + document.getElementById("userloginpassword").value);
alert(result);
if(result === "found"){
alert("In If Condition");
return true;
}
else{
alert("In else Condition");
document.getElementById("invalidusernameorpassword").style.visibility = "visible";
document.getElementById("userloginid").focus();
document.getElementById("userloginid").value = "";
document.getElementById("userloginpassword").value = "";
return false;
}
}
function ajaxForPermissionInside(url) {
var myrequest1 = new XMLHttpRequest();
myrequest1.open("POST", url, false);
myrequest1.send(null);
return(myrequest1.responseText);
}

servlet代码在这里:

response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
if(userAccount== 1)
out.println("found");
else out.println("not found");
}

请帮我找出我的javascript代码出了什么问题。

out.println()在消息后添加一个换行符。您正在将它与一个没有换行符的字符串进行比较。

请改用out.print()

相关内容

最新更新