H_MZ 2017-05-11 05:08 采纳率: 0%
浏览 40

ajax-jsp总是出错

This is my AJAX call to a servlet for login form, it executes servlet correctly and the servlet JsonObject saves the information correctly in each case (user exists or not), but AJAX always go to error function and not to success.

I can't find the problem, alert in error function shows "undefined". When user and password are correct it creates session correctly but ajax does not redirect because it's always "error".

Hope you can help me.

JS - AJAX

 function validarUsuario() {
    var usuarioR = $("#cmpUsuario").val();
    var passwordR = $("#cmpPassword").val();

    if (usuarioR.length < 1) {
            $("#divTextoEstado").html("<p>Se requiere un usuario.</p>");
            return false;
    } else if (passwordR.length < 1) {
            $("#divTextoEstado").html("<p>Se requiere una contrasenha.</p>");
            return false;
    } else {
            $.ajax({
                    type: "POST",
                    url: "IniciarSesion",
                    data: "usuario=" + usuarioR + "&password=" + passwordR,
                    dataType: "json",
                    success: function (data) {
                            //alert(data.message);
                            if (data.message.equals("Success")) {
                                    window.location.href("/principal.jsp");
                            } else {
                                    $("#divTextoEstado").html("<p>User or password are incorrect.</p>");
                                    return false;
                            }
                    },
                    error: function (data) {
                            alert(data.message);
                            console.log(data);
                            return false;
                    }
            });
    }
}

SERVLET

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");

            Usuario usuario = new Usuario();
            PrintWriter out = response.getWriter();

            String usuarioR = request.getParameter("usuario");
            String passwordR = request.getParameter("password");
            boolean correcto = usuario.validarUsuario(usuarioR, passwordR);

            if (correcto) {
                    usuario = new Usuario(usuarioR, passwordR);
                    HttpSession sesion = request.getSession(true);
                    sesion.setAttribute("nombre", usuario.getNombre());
                    sesion.setAttribute("apellido", usuario.getApellido());
                    sesion.setAttribute("usuario", usuario.getUsuario());

                    JsonObject myObj = new JsonObject();
                    myObj.addProperty("message", "Success");
                    System.out.println(myObj.get("message")); // To print value
                    out.println(myObj.toString());

            } else {
                    JsonObject myObj = new JsonObject();
                    myObj.addProperty("message", "User or password are incorrect.");
                    System.out.println(myObj.get("message")); // To print value
                    out.println(myObj.toString());

            }
            out.close();
    }
  • 写回答

2条回答 默认 最新

  • weixin_33716557 2017-05-11 05:35
    关注

    Please change :

    response.setContentType("text/html;charset=UTF-8");

    to

    response.setContentType("application/json");

    As you are expecting json result in your ajax function.

    评论

报告相同问题?