没有通过 Ajax 的 Servlet 响应
我编写了一个简单的 servlet 来处理 Ajax 请求。在服务器端,调用了doPost
,但是我在响应对象中设置的数据不会反映在客户端上。 (实际上,根据 Firebug,我没有在客户端上得到任何信息)。我使用 jQuery 来处理 Ajax。
客户端代码:
$.post(
'/mapped/url?param=' + $('#eleId').val(),
function(data){
alert(data);
},
"xml"
);
在服务器上:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("!!!In post!!!!");
// some calculations go here
response.setContentType("application/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<data><param number=\"\"></param></data>");
response.setStatus(HttpServletResponse.SC_OK);
}
提前致谢!
I've written a simple servlet for processing an Ajax request. On the server side, the doPost
is called, but the data that I've set in the response object is not reflected on the client. (Actually, I'm not getting anything on the client according to Firebug). I'm using jQuery to handle the Ajax.
Client code:
$.post(
'/mapped/url?param=' + $('#eleId').val(),
function(data){
alert(data);
},
"xml"
);
On the server:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("!!!In post!!!!");
// some calculations go here
response.setContentType("application/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<data><param number=\"\"></param></data>");
response.setStatus(HttpServletResponse.SC_OK);
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要逐步调试此问题,因为此问题可能意味着从不正确的 Servlet 配置到客户端代码中的错误等任何情况。
You need to debug this in steps as this issue could mean anything from improper Servlet configuration to a bug in the client side code.
您可能必须显式关闭输出流 -
You may have to explicitly close the output stream -