5

I've written the following Javascript code:

function sendCcbRequest(text) {
    var jsonToSend = "\"text\": \"" + escape(text) + "\"";
    $.ajax({
        type: "POST",
        url: 'x.asp',
        data: jsonToSend,
        success: function(response) {
            alert("success:" + response);
        },
        error: function() {
            alert("error");
        }
    }); // end ajax
}

How do I read the data that I post from my classic ASP code?

Update I've tried the following for my classic asp file x.asp.

<%
Dim x
x = Request.Form("text")
Response.Write(x)
%>

It still prints nothing.

Vivian River
  • 28,530
  • 54
  • 179
  • 298

4 Answers4

4

The way the data is posted using this method (as posted in the question) doesn't really create a form object on the server side. So the posted data is to be read using Request.BinaryRead and then converted to string using one of the methods given here. As you have already noted, if you send the data using query string form key1=value1&key2=value2 or a map of the form {key1: 'value1', key2: 'value2'}, the posted data is a valid form and ASP would convert it to a Request.Form that can be read much easily.

amit_g
  • 28,825
  • 7
  • 54
  • 111
3

Okay, I've found something that works. The following line of code:

var jsonToSend = "\"text\": \"" + escape(text) + "\"";

needs to be changed to

var jsonToSend = { text: escape(text) };
Vivian River
  • 28,530
  • 54
  • 179
  • 298
0

I would use the parser described here: Any good libraries for parsing JSON in Classic ASP? . It's worked for me.

Community
  • 1
  • 1
roberttdev
  • 32,556
  • 2
  • 18
  • 23
  • Perhaps for a more sophisticated task, it would be appropriate. Here, I'm just trying to read a single text value. Isn't there a method on the `Request` object I can invoke to read it? – Vivian River Mar 28 '11 at 17:00
  • In that case, I would treat it like any name:value pair passed in the request.. – roberttdev Mar 28 '11 at 17:02
0

From the x.asp page, simply use: Request.Form("text") to read the text that your Ajax request posted.

Mike Dinescu
  • 48,812
  • 10
  • 104
  • 136
  • I've tried this already, I get a blank string for the value of `Request.Form("text")`. Could the mistake be in the client-side jQuery code? – Vivian River Mar 28 '11 at 17:07
  • Do you have access to a debugging tool? Are you using FireBug? If so, take a look at the HTTP packets that you're sending to the server to see if the request looks alright. You might use FireBug, or WireShark or Fiddler to trace the HTTP request/response(s). The POST request should look something like: `text=somevalue` – Mike Dinescu Mar 28 '11 at 17:14