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"); } 
  • Probably as much as the browser tries to open inaccessible pages. Or, specify how much you yourself in the timeout parameter - Sergey

1 answer 1

First you need to make sure that the return of the ajax request occurs due to lack of time - for this we check the response status:

 error: function(jqXHR, status, e) { if (status === "timeout") { alert("Время ожидания ответа истекло!"); } else { alert(status); // Другая ошибка } } 

If the error is still related to the timeout, then you can manually set the desired value by explicitly specifying the wait value in milliseconds:

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $ .ajaxSetup (). Call is made; It can be sent to you.

The standard timeout value depends on the specific implementation of the browser, judging by the response to enSO.