Grails “render” renders the template

后端 未结 4 2047
無奈伤痛
無奈伤痛 2021-02-06 01:20

In my Grails controller I\'m responding to an AJAX call and using render to return the text:

def ajaxRandomPersonName = {
    def person = get a ran         


        
相关标签:
4条回答
  • 2021-02-06 01:54

    Make your client side javascript code handle a JSON respond and render your response with:

    render [text:"Name: ${person.name}"] as JSON

    0 讨论(0)
  • 2021-02-06 01:58

    Resolved: Adding contentType results in the template not being rendered:

    render text: "Name: ${person.name}", contentType: "text/plain"
    
    0 讨论(0)
  • 2021-02-06 02:05

    You might be getting burnt by the 'layout-by-convention' feature in Grails. If your layout name matches the controller name prefix, for example, Grails will apply the layout to every view managed by that controller. Unfortunately, it even applies to text and templates. There are currently a few JIRAs logged regarding this (see http://jira.grails.org/browse/GRAILS-7624 for example). I got burnt by this today. I resolved it by simply renaming my layout gsp such that it doesn't match any controller name. My layout was initially named 'storefront.gsp' and I have a controller named StorefrontController. I renamed the layout to 'public.gsp'.

    0 讨论(0)
  • 2021-02-06 02:06

    We've found that explicitly returning false from the action fixes this.

    I believe doing render foo as JSON returns false implicitly.

    0 讨论(0)
提交回复
热议问题