javascript - 如果 JavaScript 中的条件总是失败

标签 javascript ajax servlets if-statement

以下是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 请求,以查明输入的用户名和密码组合是否正确。如果组合正确,则返回“找到”作为响应。否则返回“未找到”。无论响应是什么...... 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()

关于javascript - 如果 JavaScript 中的条件总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572649/

相关文章:

javascript - 如何使用 javascript 回发到不同的表单

php - 在执行 AJAX 的 JS 上重新定义 SetTimeInterval,这会向服务器发送垃圾邮件吗?

投票赞成/反对系统的 Javascript 错误

java - 在 java servlet 中运行读取和写入外部文本文件的应用程序

java servlet上下文无法加载application.property文件

jsp - JSP 中的多部分表单,Glassfish 中的编码问题

javascript - 平滑地将字符移入和移出屏幕 HTML

javascript - jquery javascript 获取复选框的初始值

javascript - 连接开始后连接到 SignalR Hub

javascript - 使用 JQuery 通过 AJAX 获取文件