[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Post json to asp jscript

Andrew Poulos

8/4/2014 4:30:00 AM

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
6 Answers

Martin Honnen

8/4/2014 8:32:00 AM

0

Andrew Poulos wrote:
> 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?

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....,
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....
for the methods.


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

Andrew Poulos

8/4/2014 8:44:00 AM

0

On 4/08/2014 6:32 PM, Martin Honnen wrote:
> Andrew Poulos wrote:
>> 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?
>
> 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....,
> 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....
> for the methods.

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

8/5/2014 1:50:00 AM

0

On 4/08/2014 6:44 PM, Andrew Poulos wrote:
> On 4/08/2014 6:32 PM, Martin Honnen wrote:
>> Andrew Poulos wrote:
>>> 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?
>>
>> 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....,
>> 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....
>> for the methods.
>
> 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.

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

8/10/2014 2:22:00 AM

0

On 5/08/2014 11:49 AM, Andrew Poulos wrote:
> On 4/08/2014 6:44 PM, Andrew Poulos wrote:
>> On 4/08/2014 6:32 PM, Martin Honnen wrote:
>>> Andrew Poulos wrote:
>>>> 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?
>>>
>>> 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....,
>>> 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....
>>> for the methods.
>>
>> 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.

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.

8/10/2014 7:52:00 AM

0

Andrew Poulos <ap_prog@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_binarytos...

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

8/10/2014 10:44:00 PM

0

On 10/08/2014 5:52 PM, Evertjan. wrote:
> Andrew Poulos <ap_prog@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.

> 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_binarytos...
>
> 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>
>
> =====================

Thanks.

Andrew Poulos