Eric Darchis
Eric Darchis

Reputation: 26737

Render JSON from GSP

I have a few GSP pages that are returning JSON data. It looks like (only a lot larger)

<%@ page contentType="application/json" %>
{
  "foo": "${somevalue}"
}

And I am using this with the page renderer:

groovyPageRenderer.render view: '/renderService/foo', model: [foo: foo]

This works fine except that the values containing double quotes and other special characters are encoded with &quot; instead of \"

How can I force the encoding of these parameters to be JSON instead of HTML/XML ?

Upvotes: 2

Views: 647

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

You can change the default codec for a particular GSP page with a directive:

<%@page expressionCodec="javascript" %>

More details in the section on XSS prevention in the Grails user guide.

Upvotes: 1

Related Questions