AJAX.Request POST body not send

15,821

Solution 1

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.

Solution 2

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();
  }
});
Share:
15,821
zechariahs
Author by

zechariahs

Entrepreneur, Java programmer/engineer, project engineer, jetsetter.

Updated on June 04, 2022

Comments

  • zechariahs
    zechariahs about 2 years

    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
    
    • Tomalak
      Tomalak over 15 years
      When you set up a regular form post, no AJAX involved, does that work in the same situation? I'm asking because if you use IE, there is a non-obvious specialty to be aware of.