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"}