Question

I'm trying to create a POST request, unfortunately the body of the POST never seems to be sent.

Below is the code that I'm using. The code is invoked when a user clicks on a link, not a form "submit" button. It runs without error, invokes the servlet that is being called but, as I mentioned earlier, the body of the POST never seems to be sent.

I can validate that the request body is never sent since I have access to the servlet being called.

I've tried using "parameters" in replace of "requestBody." I've also tried using a parameter string (x=a?y=b). I've also validated that "ckULK" does contain a valid value.

Any ideas?

new Ajax.Request(sURL,
{
    method: 'POST'
    , contentType: "text/x-json"
    , requestBody: {ulk:ckULK}
    , onFailure:
        function(transport)
        {
            vJSONResp = transport.responseText;
            var JSON = eval( "(" + vJSONResp + ")" );
            updateStatus(JSON.code + ": " + JSON.message);
        } // End onFailure
    , onSuccess: 
        function(transport) 
        {
            if (200 == transport.status)
            {
                vJSONResp = transport.responseText;
            }
            else
            {
                log.value += "\n" + transport.status;
            }
         } // End onSuccess
}); // End Ajax.request
Was it helpful?

Solution

These are the kind of situations where Firebug and Firefox are really helpful. I suggest you install Firebug if you don't have it and check the request that is being sent.

You also definitely need to stick to parameters instead of requestBody.

This:

new Ajax.Request(sURL,
{
    method: 'POST',
    parameters: 'hello=world&test=yes',
    onFailure: function(transport) {
        vJSONResp = transport.responseText;
        var JSON = eval( "(" + vJSONResp + ")" );
        updateStatus(JSON.code + ": " + JSON.message);
    },
    onSuccess: function(transport) {
        if (200 == transport.status) {
            vJSONResp = transport.responseText;
        } else {
            log.value += "\n" + transport.status;
        }
     }
});

Should definitely work.

OTHER TIPS

Simply pass the data as parameters to the Ajax Request constructor:

new Ajax.Request(url, {
  method: 'POST', 
  parameters: {
    hello: "world", test: "test"
  },
  onSuccess: function(transport){
    var data = transport.responseText.evalJSON();
  }
});

When I hit this problem the solution was to remove contentType from Ajax.Request options.

In answer by Paolo Bergantino contentType is not defined in Ajax.Request options.

From unknown reasons params defined in parameters wasn't sent (FF didn't show them and my server saw empty POST body).

One of my coworkers suggested to use postBody instead of parameters but I didn't try it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top