1

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.

user1693404
  • 173
  • 1
  • 12
  • 1
    No matter what you pass into a form field, it will be treated as a string. Also an object in javascript does not automatically translate to an object in vbscript. – Jon P Feb 28 '22 at 21:10
  • 3
    "I know the JavaScript object is being passed" are you sure about that? I would be very surprised if `Response.Write(buyer)` was anything other than the string "[object Object]". You should [stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) the javascript object then see this question re using JSON in ASP/vbscript : https://stackoverflow.com/questions/12153925/decode-encode-json-with-vbscript – Jon P Feb 28 '22 at 21:20
  • @Jon P -- you're right about "[object Object"] -- that's what made me think the fully structured object was being passed. Out of curiosity, what does "Object object" refer to -- is that the actual string that's passed by the form? I didn't expect to have to get into JSON with this. – user1693404 Mar 01 '22 at 15:17
  • 1
    If you `toString()` a full JavaScript object you get "[object Object]" : https://jsfiddle.net/70rLdxsg/ . This is what is happening here as form fields can only contain strings, the object is automatically "toStringed". You need some way of serialising the object on the client end then deserializing it on the server end. JSON is one method that is easy in the borwser, not so much in classic ASP. If the object is pretty simple, I'd just have a hidden field for each property on the object. – Jon P Mar 01 '22 at 21:59

2 Answers2

0

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")
Mike Irving
  • 1,480
  • 1
  • 12
  • 20
-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
%>
silver
  • 650
  • 4
  • 15
  • More information on the down votes would help alot of people, I don't see the problem with my answer since it is loading the native JSON2 JS serverside. – silver Mar 14 '22 at 12:22