Search code examples
javascriptpostprototypejs

AJAX.Request POST body not send


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

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.