Ниже приведен код на Jsp page..This является пользователем Войти страницу ..Если условие всегда терпит неудачу в Javascript
<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(), которая вызывается при отправке формы: В этом коде, Я посылаю AJAX запрос на сервлет UserPermissionServlet, чтобы выяснить, является ли имя пользователя и пароль, введенный является правильное сочетание. Если правильная комбинация, «найденный» возвращается в качестве ответа. В противном случае возвращается «not found». Независимо от того, что ответ ... условие if всегда терпит неудачу, а утверждения в другой части исполняются. Оповещения помещаются в код только для моей справки.
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);
}
код сервлета здесь:
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
if(userAccount== 1)
out.println("found");
else out.println("not found");
}
Пожалуйста, помогите мне понять, что случилось с моим яваскриптом кодом.
«результат» фактически напечатан как '' найденный '' в вашем предупреждении? если да, попробуйте использовать только два знака '=='. Я не знаю много о jsp, но, может быть, вы можете/должны изменить 'out.println (" found ");' на что-то вроде 'return 'found';' –