How long does the .ajax method wait for a response? In my example, he is accessing the servlet, and if in the servlet before adding the response to add at least a second Thread.sleep() , then the error handler is called error , if it is removed, then the response is processed correctly. Just in a more complex example, when the servlet already in turn turns to the server, which also takes time, the answer does not come again. If at all, the concept of waiting time for a response request?
Here is the ajax request code
$(document).ready(function () { $("#login-button").click(function () { var userPassword = $("input#userPassword").val(); var userLogin = $("input#userLogin").val(); $.ajax({ type: "POST", url: "login", dataType: "text", data: {login: userLogin, password: userPassword}, success: function (data) { alert("Successful request! Data is " + data.toString()); }, error: function (jqXHR) { console.log(jqXHR); } }); }); }); The servlet method
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Даже если на секунду включить, ответ не проходит /*try { Thread.sleep(1000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); }*/ response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.getWriter().print("test"); }