Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read data posted to classic ASP with jQuery AJAX

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.

like image 709
Vivian River Avatar asked Mar 28 '11 16:03

Vivian River


2 Answers

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.

like image 84
amit_g Avatar answered Oct 16 '22 19:10

amit_g


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) };
like image 33
Vivian River Avatar answered Oct 16 '22 20:10

Vivian River