Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Post json to asp jscript

1,018 views
Skip to first unread message

Andrew Poulos

unread,
Aug 4, 2014, 12:30:16 AM8/4/14
to
I'm trying to us an elearning tracking piece of js called tincan that
posts JSON to an endpoint. The content type is set to application/json
and the request body (as IE's F12 developer console shows) is json. The
response headers give

response : HTTP/1.1 200 OK
content-length : 0
content-type : application/json; charset=utf-8

Though Chrome's developer console gives a content length of 16568.

When (on the server with classic ASP and JScript) I try

var dat = Request.Form;
or
var dat = Request.Form + "";
or
var dat = eval("(" + Request.Form + ")");

I just get 'undefined'.

My question is, if the payload is not empty why does request give undefined?

Andrew Poulos

Martin Honnen

unread,
Aug 4, 2014, 4:32:27 AM8/4/14
to
As far as I remember, with classic ASP the Request.Form collection is
provided for reading out the values posted by a HTML form. If you
directly post JSON then I think you need to read out the request body
with http://msdn.microsoft.com/en-us/library/ms525710%28v=vs.90%29.aspx,
only the object you get from that is a SafeArray that does not fit in
well with the JScript data types. So you need something along the lines of

var responseArray = new
VBArray(Request.BinaryRead(Request.TotalBytes)).toArray();

See
http://msdn.microsoft.com/en-us/library/ie/y39d47w8%28v=vs.94%29.aspx
for the methods.


--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Andrew Poulos

unread,
Aug 4, 2014, 4:44:14 AM8/4/14
to
I wishfully thought I could just use your code without without having to
do any work. Alas, I get "error '800a1395' VBArray expected".

Thanks for the advice. Now I have a direction to work to.

Andrew Poulos

Andrew Poulos

unread,
Aug 4, 2014, 9:49:36 PM8/4/14
to
Unfortunately I can't get it to work - even trying to rewrite the page
using vbscript proved problematic - so instead I've modified tincan.js
so that when it posts it does so as a "form". Its still sending json but
not as application/json.

Andrew Poulos

Andrew Poulos

unread,
Aug 9, 2014, 10:21:48 PM8/9/14
to
To anyone that might be interested, here's one "solution":

<%@ language="javascript" %>

<script runat="server" language="vbscript">
Dim vntPostedData, lngCount, bString

lngCount = Request.TotalBytes
vntPostedData = Request.BinaryRead(lngCount)
bString = binaryToString(vntPostedData)

Function binaryToString(Binary)
'SimpleBinaryToString converts binary data
' (VT_UI1 | VT_ARRAY Or MultiByte string)
' to a string (BSTR) using MultiByte VBS functions
Dim I, S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
BinaryToString = S
End Function
</script>

<script runat="server" type="text/javascript" language="javascript">
Response.Write(bString);
</script>

Apparently in a Classic ASP page the non-default language script gets
run before the default language script.

Andrew Poulos

Evertjan.

unread,
Aug 10, 2014, 3:52:27 AM8/10/14
to
Andrew Poulos <ap_...@hotmail.com> wrote on 10 aug 2014 in
comp.lang.javascript:

> Apparently in a Classic ASP page the non-default language script gets
> run before the default language script.

Therefore there is confusing to set your
Response.Write(bString);
in its own section.

And:

1 it is customary to set the non-default language sections at the bottom,
and only define functions there to be called in the main script.

2 VBS 'Dim'-ing without 'Option Explicit' set is only window dressing.

3 for large binaries read this for faster response:
<http://www.motobit.com/tips/detpg_binarytostring/>

Try, [not tested]:

=====================
<%@ language="javascript" %>

Response.Write(bString);

<script runat="server" language="vbscript">

Function bString()
lngCount = Request.TotalBytes
vntPostedData = Request.BinaryRead(lngCount)
bString = binaryToString(vntPostedData)
End Function

Function binaryToString(Binary)
S = ""
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
BinaryToString = S
End Function

</script>

=====================


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Andrew Poulos

unread,
Aug 10, 2014, 6:44:26 PM8/10/14
to
On 10/08/2014 5:52 PM, Evertjan. wrote:
> Andrew Poulos <ap_...@hotmail.com> wrote on 10 aug 2014 in
> comp.lang.javascript:
>
>> Apparently in a Classic ASP page the non-default language script gets
>> run before the default language script.
>
> Therefore there is confusing to set your
> Response.Write(bString);
> in its own section.

I needed to be sure that the vbs value was available to js.
Thanks.

Andrew Poulos

0 new messages