我有这个问题,我昨天已经问过这个问题但我没有任何答案...... :(
我在客户端有这段代码:
var formdata = new FormData();
//fill fields of formdata... for example:
var file = document.getElementById("file").files[0];
formdata.append("file", file);
//and others....but the problem is not here
var xhr = new XMLHttpRequest();
xhr.open("POST","http://127.0.0.1:8080/Commerciale",true);
xhr.send(formdata);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var str = xhr.responseText;
alert(str);
}
}
});
到目前为止,这似乎是公平的。在 servlet 中,我有以下代码:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
***other code, but i think that the problem is here:
PrintWriter ajaxWriter = response.getWriter();
ajaxWriter.println(p.getJSON());
ajaxWriter.flush();
System.out.println(p.getJSON());
ajaxWriter.close();
}
问题在于
System.out.println(p.getJSON());
打印出我的期望,但似乎
xhr.responseText
不返回任何东西,实际上警报是空的。
有人可以解释我为什么吗?