Search code examples
ajaxgrailsgsp

Ajax using <g:remoteLink> in Grails


Reading through the Grails docs (see here http://grails.org/doc/latest/guide/theWebLayer.html#ajax), I was led to believe that I could use Ajax to update a div using the following syntax:

My view (Ajax/index.gsp)

<!doctype html>
<head>
        <meta name="layout" content="main"/>
</head>
<body>
        <div id="error"></div>
        <div id="message"></div>
        <g:remoteLink action="retrievePets" update="message">Ajax magic... Click here</g:remoteLink>
</body>
</html>

My controller (AjaxController):

package genericsite 

class AjaxController { 

    def index() { } 

        def retrieveMessage() { 
                render "Weeee! Ajax!" 
        } 
  } 

However, when I select the link, it just sends me to a page with "Weeee! Ajax!" I know how to do this the typical jQuery way. This is slightly more convenient...


Solution

  • The default "main" layout doesn't include a javascript library by default, so if you want to use remoteLink or any of its associates you'll need to add

    <r:require module="jquery"/>
    

    or (if you're on a pre-2.0 version of Grails or not using the resources plugin)

    <g:javascript library="jquery"/>
    

    to the <head> section of your GSP.