The Ajax request successfully receives a response from the servlet if the servlet contains only one line - response.getWriter().print("test"); . Why, when trying to configure the contents of response or when trying to close a stream, the response does not pass and the error handler error with the same status is triggered (result console.log(jqXHR) = Object {readyState: 0, responseText: "", status: 0, statusText: "error"} )? (In fact, the answer does not pass when you try to add any code other than the specified line, always the error status is the same.)
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); } }); }); }); Servlet code
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //При попытке добавить этот код /*response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8");*/ response.getWriter().print("text"); //Или этот код ответ уже не проходит /*response.getWriter().flush(); response.getWriter().close();*/ }
catch(Exception ex)- will_hunting