Question

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.

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top