'Submitting a javascript object to vbScript and referencing its properties in classic ASP

I've never passed a JavaScript object to an .asp page. Can someone give me the syntax here. I've tried googling w/o success. The object is called buyerInfo. Using jQuery.

<form id="aform" action="formact.asp" method="POST">
    <input type="hidden" id="myhid" name="myhid" value="">
    <input type="submit">
</form>

<script>
    buyerInfo = {};
    buyerInfo.name = "Joe Buyer"
    buyerInfo.zip = "12345"
    $("#myhid").val(buyerInfo);
</script>

-- and in formact.asp

<%
    Set buyer = Request("myhid")
    name = buyer.name
%>

yields Object doesn't support this property or method: 'buyer.name'.

What is the correct way to reference these? I know the JavaScript object is being passed, but I don't know how to access its pieces. Thanks.



Solution 1:[1]

You could get away without using any Classic ASP libraries

jQuery

    $.post('/process.asp', {
        name: buyerInfo.name,
        zip: buyerInfo.zip
    }, function (data) {
    }).done(function (data) {
        alert(data); // Data that is returned by the ASP pae
    });

ASP

dim name : set name = request.form("name")
dim zip: set zip= request.form("zip")

Solution 2:[2]

Classic ASP does not have JSON or object's (at least in this sense) built-in by default and you have to load other libraries.

I suggest including this ASP JSON library at the top of formact.asp :

https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp

and then passing the value like this:

<%
    Dim buyer : set buyer = JSON.parse(request.form("myhid"))
    name = buyer.name
%>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike Irving
Solution 2 silver