【发布时间】:2012-10-04 12:42:09
【问题描述】:
我有这个问题,我昨天已经问过这个问题但我没有任何答案... :(
我在客户端有这段代码:
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
什么都不返回,其实alert是空的。
谁能解释一下为什么?
【问题讨论】:
-
你用哪个浏览器试过这个?
-
尽量不要关闭编写器(这应该不是问题 - 但你也不需要它)
-
现在可以了!!!谢谢!如果您写下问题的答案,我会注意到您的答案是正确的。 :)
-
当然 - 谢谢:) stackoverflow.com/a/12881237/357360
标签: java httpresponse xmlhttprequest-level2