This issue was encountered while using Grails version 2.3.11
In theory it should be as simple as:
- Return JSON in your model from your controller
def index() = {
[data: data as JSON]
}
- Render the model attribute without encoding using the raw() method
<r:script>
var data = ${raw(data)};
</r:script>
Unfortunately this does not work, at least when you are using the resources plugin. You know it does not work because all the quotes have been escaped and they should have not:
<script>
var data = {"whatever":"whatever"};
<script>
It took me a while to find a way of doing it using the applyCodec taglib. Just replace the previous second step with this:
<r:script>
<g:applyCodec encodeAs="none">
var data = ${data};
</g:applyCodec>
</r:script>
as a result you will see that is has been rendered properly:
<script>
var data = {"whatever":"whatever"};
<script>
Link to related issues:
Update
You can also use gsp page directives like these ones to set the encoding mechanism on a single page basis:
- Remove enconding from EL expressions:
<%@ expressionCodec="none" %>
- Remove encoding from scriplets:
<%@ scriptletCodec="none" %>
- Remove encoding everywhere in the page:
<%@ defaultCodec="none" %>