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.

4
  • 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. Commented Feb 28, 2022 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 the javascript object then see this question re using JSON in ASP/vbscript : stackoverflow.com/questions/12153925/… Commented Feb 28, 2022 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. Commented Mar 1, 2022 at 15:17
  • 1
    If you toString() a full JavaScript object you get "[object Object]" : 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. Commented Mar 1, 2022 at 21:59

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

Comments

-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
%>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.