如果条件总是在 Javascript 中失败

If Condition always fails in 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 函数 returns 为真,则在提交上述表单后,它会导航到另一个 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" 作为响应。否则返回 "not 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()