2

I am trying to pass the contents of a bean to javascript so that I can parse it and create a JSON object... (Yes I am still on ATG 9.1). However I am having trouble getting from serverside to client side.... I am new with this stuff and would appreciate any explanation as documentation on this is scarce and not helpful.

<dsp:tomap var="cartMap" bean="MyShoppingCartModifier.order" recursive="true"/>
<script>
    var myCartMap = "${cartMap}";
    //Logic (easy)
</script>

Doing this generates an "Uncaught SyntaxError: Unexpected token ILLEGAL" on my browser (Chrome)

Any wisdom will greatly help me in my quest in learning this stuff.

1 Answer 1

2

The problem is your usage of the tomap tag. You can't just pass in an entire tomap'd object because the tomap tag isn't going to create a nice, parsable json object.

You should either:

1) Format the json yourself right within your tags. Choose only the values that you want from the order.

<script>
   var myCart = {
      total : '<dsp:valueof bean="MyShoppingCartModifier.order.priceInfo.total">'
      ...
   }

   // Then use myCart for something here
</script>

or 2) There's a little known JSP to JSON library found here, http://json-taglib.sourceforge.net, that is very useful. To use that, you'd create a separate page, something like orderJSON.jspf, that is used to generate a pure json object from your order. Then in the page that you require this js, you can do:

<script>
   var myCart = <%@ include file="/path/to/orderJSON.jspf" %>
   // Then use myCart for something here.
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice, thanks for your help. The first suggestion worked like a charm!

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.