我正在尝试使用ajax和servlet进行一些测试,但是当我启动应用程序时,servlet总是返回'error'
SELECT *
FROM tablename
WHERE nvl(productloc,'zzz') = (case when productloc is null then 'zzz' else productloc end )
package servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "ServletCatalogue", urlPatterns = "/test")
public class ServletCatalogue extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write("test");
}
}
为什么总是打印$(function() {
$("#catalogue").on('click', function() {
$.ajax({
url: "/test",
type: "GET",
success: ok,
error: error
});
});
});
function ok() {
alert("fine")
};
function error() {
alert('error')
}
?我知道JS正确调用了servlet,因为我放了一个"error"
来检查它。
答案 0 :(得分:0)
我正在使用Java Servlet接收呼叫。首先弄清楚是什么原因。我使用以下功能做到了
function ajaxFailure(data, textStatus, errorThrown){
alert("OH NO! data: "+data + ' textStatus: ' + textStatus + 'errorThrown: ' + errorThrown);
}
我发现错误是由于errorThrown:没有从文本到application / json的转换 然后在$ .ajax调用中将 application / json 更改为 text 。这解决了我的问题