我无法比较来自servlet的ajax请求返回的字符串

时间:2018-06-12 19:34:12

标签: javascript java ajax jsp servlets

任何人都可以帮我这个吗? 第一个if语句完全正常 但第二个如果永远不是真的 这意味着无论什么时候我打印'不'它都很好但是什么时候打印'确定'它不起作用,显然如果我使用第一个,如果那么它也不起作用

<script type="text/javascript">
    function signupfunction(){
        var name = document.getElementById('signupname').value;
        var email = document.getElementById('signupemail').value;
        var password = document.getElementById('signuppassword').value; 
        $.ajax({
            url : "AdminSignup?name=" + name + "&email=" + email + 
"&password=" + password + "",
            type : "GET",
            async : true,
            success : function(data) {
                if(data == 'not'){
                    alert("Email already exists");
                } 
                if(data == 'ok'){
                    alert('hello');
                }
            }
        });
    }
</script>

这是我的servlet的doGet()函数

protected void doGet(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost/amazon? 
                user=root&password=test123");
            PreparedStatement statement = con.prepareStatement("select * from 
                sellers where email=?");
            statement.setString(1, request.getParameter("email"));
            ResultSet rs = statement.executeQuery();
            if(rs.next()) {
                response.getWriter().print("not");
            } else {
                PreparedStatement statement1 = con.prepareStatement("insert 
                    into sellers(email,password,name) values(?,?,?)");
                statement1.setString(1, request.getParameter("email"));
                statement1.setString(2, request.getParameter("password"));
                statement1.setString(3, request.getParameter("name"));
                statement1.executeUpdate();
                session.setAttribute("sessionAdminEmail", 
                    request.getParameter("email"));
                session.setAttribute("sessionAdminName", 
                    request.getParameter("name"));
                response.getWriter().print("ok");
            }
        } catch (Exception e) {

          }
    }

1 个答案:

答案 0 :(得分:0)

你正在混淆

response.getWriter().print("ok"); //this will not send your value to ajax

使用:

response.getWriter().write("inserted new user");   //this will

您的Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws ServletException, IOException {
            HttpSession session = request.getSession();
            response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
            response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
 try {


        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/amazon?user=root&password=test123");

        PreparedStatement statement = con.prepareStatement("select * from sellers where email=?");
        statement.setString(1, request.getParameter("email"));
        ResultSet rs = statement.executeQuery();

        if(rs.next()) {
            //if the email exists in sellers, then this will always evaluate to true...
            //response.getWriter().print("not"); //print will not send your value back to ajax..
            response.getWriter().write("email exists");       // Write response body.
        } else {
            PreparedStatement statement1 = con.prepareStatement("insert into sellers(email,password,name) values(?,?,?)");
            statement1.setString(1, request.getParameter("email"));
            statement1.setString(2, request.getParameter("password"));
            statement1.setString(3, request.getParameter("name"));
            statement1.executeUpdate();
            session.setAttribute("sessionAdminEmail", request.getParameter("email"));
            session.setAttribute("sessionAdminName", request.getParameter("name"));
            //response.getWriter().print("ok"); print will not send your value back to ajax
            response.getWriter().write("inserted new user");       // Write response body.
        }
    } catch (Exception e) {

      }
}

另外值得一提的是=====不同。在这种情况下使用===,它是您正在寻找的比较运算符。查看this以获取有关差异的说明

<script type="text/javascript">
    function signupfunction(){
        var name = document.getElementById('signupname').value;
        var email = document.getElementById('signupemail').value;
        var password = document.getElementById('signuppassword').value; 
        $.ajax({
            url : "AdminSignup?name=" + name + "&email=" + email + 
"&password=" + password + "",
            type : "GET",
            async : true,
            success : function(data) {
                if(data === 'email exists'){
                    alert("Email already exists");
                } 
                if(data === 'inserted new user'){
                    alert('new user');
                }
            }
        });
    }
</script>

同时查看这个答案,它给出了如何将Ajax与Servlet一起使用的一个很好的解释。 (比你目前正在做的方式容易得多)

  

How to use Servlets and Ajax?