我有jquery脚本,它向servlet发送ajax请求。它工作,它将文本数据正确地发送到servlet ,但随后它调用错误函数,而不是成功函数(我检查过,servlet发送回ajax not null 字符串)。
为什么ajax方法调用错误函数?
这里的脚本代码
$(document).ready(function () {
$("#login-button").click(function () {
var userPassword = $("input#userPassword").val();
var userLogin = $("input#userLogin").val();
$.ajax({
type: "POST",
url: "http://localhost:8181/library/login",
data: {login: userLogin, password: userPassword},
dataType: "text",
success: function (data) {
if (data == "1") {
document.location.href = "http://localhost:8181/library/workshop.html";
}
if (data == "2") {
document.location.href = "http://localhost:8181/library/library.html";
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Error report\n" + "jqXHR = " + jqXHR + "\n" + "textStatus = " + textStatus + "\n" +
"errorThrown = " + errorThrown);
}
});
});
});
这里的servlet代码
public class LoginServlet extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String login = request.getParameter("login");
String password = request.getParameter("password");
SocketConnection.output.println("log_in " + login + " " + password);
out.print(SocketConnection.input.readLine());
out.close();
}
}
textStatus值为error
,错误报告中的errorThrown值为void。
答案 0 :(得分:0)
如果它返回状态200 您的函数包含以下设置
dataType: "text",
可能没有发送文字。只是尝试删除此设置,我希望它能正常工作