views:

3273

answers:

4

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
+2  A: 

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.

Paolo Bergantino
A: 

Thanks for your response Paolo. Unfortunately it really didn't help. The problem seems to exist with Prototype. If I go back to strict Javascript (example below) everything works fine.

Also, thanks for the Firebug recommendation. It does have some really nice features!

function postMessage()
{
    var ckULK = getCookie(ULK_COOKIE_NAME);

    var req = new XMLHttpRequest();

    var params = "?ulk=" + ckULK;

    var sURL = sBaseURL + MESSAGE_POST + params;

    req.open('POST', sURL, true);

    req.setRequestHeader("Content-type", "text/x-json");
    req.setRequestHeader("Content-length", params.length);
    req.setRequestHeader("Connection", "close");


    req.onreadystatechange = function (aEvt) 
    {
        if (req.readyState == 4) 
        {
            if(req.status == 200)
            {
                try
                {
                    var vJSONResp = req.responseText;
                    var JSON = eval( "(" + vJSONResp + ")" );
                    updateStatus(JSON.message);
                    loadYouMessages();
                }
                catch(e)
                {
                    return e;       
                }
            }
        }
    };

    req.send(params);

}
zechariahs
Have you checked with Firebug that a request is being sent when you expect it to? The example I posted above worked properly with prototype 1.6.0.2
Paolo Bergantino
A: 

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();
  }
});
aemkei
A: 

I'm having an almost very similar problem:

<html>
<head>
    <script type="text/javascript" src="prototype.js"></script>
</head>
<body>
    <div id="result"></div>
    <script type="text/javascript">
        new Ajax.Request('http://tinyurl.com/api-create.php',{
            method:'GET',
            parameters: {url: 'www.phpfreaks.com',callback: 'y'},
            onSuccess: function(transport){alert("onSuccess: " + transport.responseText)},
            onFailure: function(){ alert('Something went wrong...') }
    });
    </script>
</body>

I cannot for the life of me get a response back. Any suggestions?

Matt Torbin