我正在尝试使用以下代码在 Controller 中创建自定义json输出,但在chrome rest客户端中收到错误“意外 token <”。相同的代码适用于xml。
def customJSON = {
def a = Student.list().get(0)
render(contentType:"application/json"){
student(){ name(a.firstName) }
}
}
def customXml = {
def a = Student.list().get(0)
render(contentType:"text/xml"){
student(){ name(a.firstName) }
}
}
最佳答案
您的代码导致以下异常:
Message: Array elements must be defined with the "element" method call eg: element(value)
Line | Method
->> 98 | invokeMethod in grails.web.JSONBuilder
问题是grails发送带有异常内容但内容类型为'application / json'的HTML响应。因此客户端认为这是无效的JSON响应。
下面的代码应该工作:
def a = Student.list().get(0)
render(contentType:"application/json"){
student(name : a.firstName)
}
关于json - Grails-在 Controller 中生成自定义JSON时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380157/