开发者

No servlet response via Ajax

I've written a simple servle开发者_如何学Got 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!


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.

  1. What status code are you seeing in firebug for the XHR (AJAX) request? (anything other than 200 is a red flag. check server logs)
  2. Is your system.out statement getting executed? i.e. is "!!!In post!!!!" logged?
  3. Create a simple html with a form that posts to the servlet and see if you get any results back.
  4. Depending on the results for above steps, debug further if required.


You may have to explicitly close the output stream -

  PrintWriter out = res.getWriter();

  out.println( "Sample response" );
  out.close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜