I have a simple ajax request that sends data to the servlet, and the servlet, regardless of this data, sends a string in response. Why, despite the fact that the ajax request successfully sends data to the servlet, does the error function work for it each time, as if the request was not successful?

Script code

 $(document).ready(function () { $("#login-button").click(function () { var userPassword = $("input#userPassword").val(); var userLogin = $("input#userLogin").val(); $.ajax({ type: "POST", url: "login", data: {login: userLogin, password: userPassword}, success: function (data) { alert("Successful request!"); if (data == "1") { document.location.href = "http://localhost:8181/library/library.html"; } if (data == "2") { document.location.href = "http://localhost:8181/library/library.html"; } }, dataType: "text", error: function (jqXHR, textStatus, errorThrown) { alert("Error report\n" + "jqXHR = " + jqXHR.responseText + "\n" + "textStatus = " + textStatus + "\n" + "errorThrown = " + errorThrown); } }); }); }); 

Servlet code

  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String login = request.getParameter("login"); String password = request.getParameter("password"); SocketConnection.output.println("log_in " + login + " " + password); //ajax запрос присылает корректные значения, все хорошо //попытка отослать обратно строку response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.getWriter().println("1"); } 

UPD. The alert message, textStatus is set to error , errorThrown empty, jqXHR.status = 0 .

UPD 2. Object {readyState: 0, responseText: "", status: 0, statusText: "error"}

  • almost a beautiful question. What did you not get from the alert message? - Igor
  • Added a message - will_hunting
  • jqXHR must have statusCode (). Error code (or success). It immediately shows what the nature of the problem is - Sergey
  • The error code is 0 - will_hunting
  • one
    Does SocketConnection work? Maybe an exception does not allow the answer code - Sergey

0