Search code examples
ajaxjsonprototypejs

Reading custom POST body within ASP


I'm using Prototype 1.6.1 to create a POST to a page. The POST data is a complex JSON object. Can someone tell me how on the receiving page I can access the raw body of the POSTed data?

Sending page:

myObject = {
  "key": "val",
  "has many": {
    "key1": "val1",
    "key2": "val2"
  }
}

new Ajax.Request('Worker.asp', {
  method: "post",
  postBody: Object.toJSON(myObject),
  onSuccess: function(transport) {
    var response = transport.responseText || "no response text";
    alert("Echo'ing back what you sent: \n\n" + response);
  },
  onFailure: function() {
    alert('Something went wrong...')
  }
});

So that's the sending page. Makes an object, and a request. I've used FireBug to ensure that the POST data being sent looks like what I want it to look like.

Now on the target page, I'm having trouble accessing the POSTed data. I tried the following, and it did not work.

Receiving page:

<% Response.BinaryWrite(Request.BinaryRead(Request.TotalBytes)) %>

But I get server error 500. So basically, I'd like to know how I can use what I have POSTed. Any help is greatly appreciated!


Solution

  • Ok, I think I have it: I was trying to force myself upon poor POST, when all she really wanted was to be treated like a lady. Instead of making my POST data be just the JSON "text", I make it a parameter:

    new Ajax.Request('StageWorker.asp',
    {
     method:"post",
     //postBody:Object.toJSON(AllStageInfo),  //<-- THIS DIDN'T WORK
     parameters:{alldata:Object.toJSON(AllStageInfo)}, //<-- THIS DID
     onSuccess: function(transport){
       var response = transport.responseText || "no response text";
       alert("Success! \n\n" + response);
       },
     onFailure: function(){ alert('Something went wrong...') }
    });
    

    And the receiving page was simply:

    Response.Write( Request.Form("alldata"))
    

    And tada, the request alerted me back with what I had sent.