Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing map from grails to javascript

My problem is passing Map object from grails controller to JavaScript. I have the following code inside controller

def scoreValue=new HashMap<String,String>();
scoreValue.put("0","poor");
scoreValue.put("1","good");
...

return (view:'viewname',model:[scoreValue:scoreValue]);

I have been searching for solution and have got this link pass a groovy array to javascript code. but could not help.

What I did was change the return statement to

return (view:'viewname',model:[scoreValue:scoreValue as grails.converters.JSON]) and inside gsp view I have the following code.

<g:if test="${scoreValue}">
     var scoreValue=${scoreValue};
</g:if>

But what i got inside html page is the following

 var scoreValue={&quot;0&quot;:&quot;Failure&quot;,&quot;1&quot;:&quot;Poor&quot;}

any help would be appreciated. thanks!

like image 331
Mitiku Avatar asked Apr 19 '26 11:04

Mitiku


1 Answers

There's actually a few ways of handling GSP encoding. In addition to D. Kossatz's answer, these methods will help you out (see more at mrhaki's excellent Grails Goodness blog)

 var scoreValue=${raw(scoreValue)};

 var scoreValue=${scoreValue.encodeAsRaw()}

Please be aware that there is an inherent risk of cross-site scripting vulnerabilities when rendering user input unprotected on the page. So long as you know for certain that only you can set that value, and proper safe-checks to ensure it is what it is supposed to be, you should be fine.

like image 52
ZacharyAKlein Avatar answered Apr 21 '26 00:04

ZacharyAKlein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!