maximus
maximus

Reputation: 2427

No servlet response via Ajax

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!

Upvotes: 2

Views: 2531

Answers (2)

Pushkar
Pushkar

Reputation: 7580

You may have to explicitly close the output stream -

  PrintWriter out = res.getWriter();

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

Upvotes: -1

Amol Katdare
Amol Katdare

Reputation: 6760

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.

Upvotes: 4

Related Questions