我向 servlet 发送了一个 ajax 请求,它显示 500 内部服务器错误java.lang.NullPointerException
。但它成功发布 {"word":"value"} 。如果它通过 AJAX 调用成功地从客户端发布数据,它应该是我的 servlet 的东西。但无法弄清楚它到底是什么。
AJAX 调用
function sendAjax() {
// get inputs
var word = {
word:$('#word').val()
}
$.ajax({
url: "WordQuest",
type: 'POST',
dataType: 'json',
data: JSON.stringify(word),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
$('#shuffled').append(data);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
小服务程序
public class WordQuest extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
String requset_word = request.getParameter("word");
WordShuffle cls = new WordShuffle();
String shuffled_word = cls.shuffle(requset_word);
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(shuffled_word);
out.flush();
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
这是堆栈跟踪
java.lang.NullPointerException
at WordShuffle.str_to_arr(WordShuffle.java:22)
at WordShuffle.shuffle(WordShuffle.java:11)
at WordQuest.doGet(WordQuest.java:20)
at WordQuest.doPost(WordQuest.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)