1

From my client side code, I am making an AJAX call to my servlet. If I use GET as request method. Everything works and I get response back. But when I send request as POST, servlet fails to send the response. From log I found out that in servlet "request" object is null when made ajax call with POST. According to this post: Servlet response to AJAX request is empty , I'm setting headers for same-origin policy.

Below is my code for reference:

function aimslc_ajaxCall(url,callback, postParams){
  var xmlhttp = null
  if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
  }
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
    eval( callback+"("+xmlhttp.responseText+")" );
    }
  }

  if(postParams!=null && typeof postParams!="undefined" ){
            xmlhttp.open("POST",url,true);
    xmlhttp.send(postParams);
  }else{
            xmlhttp.open("GET",url,true);
        xmlhttp.send();
  }
}

Servlet Code:

 public void doProcess (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      logger.info("doProcess::start..."+request.getQueryString());
      response.setHeader("P3P","CP='NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM'");
  response.setHeader("Access-Control-Allow-Origin","*");
  response.setHeader("Access-Control-Allow-Credentials","true");
  response.setHeader("Access-Control-Allow-Methods","POST, GET");
 }

Throws a null exception on request.getQueryString()

Community
  • 1
  • 1
indusBull
  • 1,834
  • 5
  • 27
  • 39
  • Check your if statement in ajax, Seems like it always executes xmlhttp.send() without any payload. Try to put alert to check which conditional option is being chooses every time. – TeaCupApp Sep 01 '11 at 01:00
  • Actually if there are no params, I make a request with GET method. Edited code to make it more clear – indusBull Sep 01 '11 at 01:13

1 Answers1

3

if you do a post all the data is in the request body, not on the url. From here you see that getQueryString only gets the stuff on the url.

See here for how to get the request body.

Also, if your data is name/value pairs, you might want to use getParameter and associated methods.

If the request is null, I ask do you implement doPost on your servlet?

Community
  • 1
  • 1
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • This makes some sense. I do have doPost(). Following on the same question - what's difference between "request.getAttribute()" and "request.getParameter()". In servlet that I m using, request.getAttribut() is used to retrieve request parameters. This seems to work for GET but not for POST. – indusBull Sep 01 '11 at 01:49
  • getParameter is used to get parameters of the from key=val. get attribute gets the request attributes, which are not the same. You want to use getParameter and associated methods – hvgotcodes Sep 01 '11 at 02:04
  • @indusBull: the `HttpServletRequest` has `getParameter()` and `getAttribute()` methods. Which one do you *think* that it returns the request **parameter**? Try to think logically... – BalusC Sep 01 '11 at 02:42
  • @BalusC My logical conclusion before asking that question was getParameter(). But the servlet (written by some well exp person) that I'm accessing has getAttributes() methods.That confused me. – indusBull Sep 01 '11 at 04:35
  • It's just been used to get request **attributes**. Not parameters. – BalusC Sep 01 '11 at 04:41
  • I've opened an another question to discuss the difference. http://stackoverflow.com/questions/7266338/why-does-getattribute-in-httpservletrequest-works-for-get-mothod-but-not-for-po – indusBull Sep 01 '11 at 05:14