0

我正在尝试使用以下代码在控制器中创建自定义 json 输出,但在 chrome rest 客户端中收到错误“Unexpected 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) }
    }

}
4

1 回答 1

1

您的代码导致以下异常:

Message: Array elements must be defined with the "element" method call eg: element(value)
    Line | Method
->>   98 | invokeMethod      in grails.web.JSONBuilder

问题是 grails 发送带有异常内容的 HTML 响应,但内容类型为“application/json”。所以客户端认为这是一个无效的 JSON 响应。

以下代码应该可以工作:

def a = Student.list().get(0)

render(contentType:"application/json"){
            student(name : a.firstName)
        }
于 2013-03-13T08:47:38.397 回答