Get Classic ASP variable from posted JSON
Solution 1
alphadogg's solution didn't work for me, I got errors with the line bStream.Write requestBody
(saying "Operation is not allowed in this context.") This seems to work for me, and returns the whole request string. However, it will only work for request data <= 100 KB, otherwise you'll have to work out how to get the BinaryRead method working.
str = Request.Form
(Discovered from http://msdn.microsoft.com/en-us/library/ms525985%28v=VS.90%29.aspx)
Solution 2
Here is the solution that i used in ASP Vbscript with Radium's code and some corrections:
bytecount = Request.TotalBytes
bytes = Request.BinaryRead(bytecount)
Set stream = Server.CreateObject("ADODB.Stream")
stream.Type = 1 'adTypeBinary
stream.Open()
stream.Write(bytes)
stream.Position = 0
stream.Type = 2 'adTypeText
stream.Charset = "utf-8"
s = stream.ReadText() 'here is your json as a string
stream.Close()
Set stream = nothing
Response.write(s)
Solution 3
alphadogg's code worked for me, but only after I specified a little more information:
bytecount = Request.TotalBytes
bytes = Request.BinaryRead(bytecount)
Set stream = Server.CreateObject("ADODB.Stream");
stream.Type = 1; // adTypeBinary
stream.Open();
stream.Write(bytes);
stream.Position = 0;
stream.Type = 2; // adTypeText
stream.Charset = "utf-8";
Set s = stream.ReadText();
stream.Close();
Prior to this I would get "Operation is not allowed in this context." as jjokin reported.
Solution 4
I having same issue but very soon i figure out how to post a json object to ASP server.
Here is the idea: not tested. Work hard on it ><.
Parse the json object into string then post back to server.(this is a typical string posting)
on ASP side, use JSON library to parse the string back into the object.
link for the JSON library http://code.google.com/p/aspjson/
'I assume ppl that try post JSON back to asp should have basic posting knowledge. Any further assistance please let me know
Solution 5
You may consider switching from VBScript to JScript (JScript is what microsoft calls JavaScript).
Why? Because then from within Classic ASP you can do the same JSON calls that the browser can do and read the results into JavaScript objects using the eval() statement.
Paddy Said: I don't have an answer, but you have my every sympathy... classic asp and JSON handling - sounds like fun.
@Paddy: Classic ASP and JSON IS fun, in fact it ROCKS! (If you switch from VBScript and use JScript.)
Note that you don't have to quit VBScript cold-turkey, you can still interoperate between the two in the same ASP file but if you declare JScript first you need to confine your VBScript to SUB or functions and vice-versa otherwise unpredictable things can happen.
Here's a quick example of what I'm talking about:
<%@ LANGUAGE="JScript" %>
<%
var days = VBDateDiff("d", "4/10/2010", "5/3/2010");
Response.write("JScript Calling VBScript function: days = " + days);
%> <script language="VBScript" runat="server">
function VBDateDiff(units, datebefore, dateafter)
VBDateDiff = CStr(DateDiff(units, datebefore, dateafter))
end function
function VBDateAdd(units, nUnits, theDate)
Response.write("<BR>VBDateAdd units=" & units & ", nUnits=" & nUnits & ", theDate=" & theDate)
VBDateAdd = CStr(DateAdd(units, nUnits, theDate))
Response.write(", VBDateAdd=" & VBDateAdd)
end function
</script> <%
%>
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
I'm trying to post JSON via AJAX to a Classic ASP page, which retrieves the value, checks a database and returns JSON to the original page.
I can post JSON via AJAX. I can return JSON from ASP. I can't retrieve the posted JSON into an ASP variable.
POST you use Request.Form, GET you use Request.Querystring. What do I use for JSON?
I have JSON libraries but they only show creating a string in the ASP script and then parsing that. I need to parse JSON from when being passed an external variable.
Javascript
var thing = $(this).val(); $.ajax({ type: "POST", url: '/ajax/check_username.asp', data: "{'userName':'" + thing + "'}", contentType: "application/json; charset=utf-8", dataType: "json", cache: false, async: false, success: function() { alert('success'); } });
ASP file (check_username.asp)
Response.ContentType = "application/json" sEmail = request.form() -- THE PROBLEM Set oRS = Server.CreateObject("ADODB.Recordset") SQL = "SELECT SYSUserID FROM dbo.t_SYS_User WHERE Username='"&sEmail&"'" oRS.Open SQL, oConn if not oRS.EOF then sStatus = (new JSON).toJSON("username", true, false) else sStatus = (new JSON).toJSON("username", false, false) end if response.write sStatus
-
Ben almost 14 yearswouldn't it be really dangerous to do a server side eval of a json string?
-
alphadogg almost 13 yearsThe problem is that when JSON is POSTed to a classic ASP page, it doesn't parse it into the Form object. Likewise if you have it in a GET without tying it to a paramter.
-
alphadogg almost 13 yearsYes, but how do you access the POSTed data?