Search code examples
ajaxgrailsgroovyprototypegsp

Grails ajax functionality


I'm fairly new to Grails and today was following a tutorial on spicing up Grails with Ajax (prototype). The tutorial was simple quote of the day Grails app that would asynchronously fetch random quote from the db. I've spent almost 3 hours getting ajax to work, finally got it to work with weird outcome. Basically, ajax wouldn't work unless I put the following two lines to both gsp of the view and gsp of the layout:

<g:javascript library="prototype" />
<r:layoutResources />

I tried to put the above lines into either one, but that wouldn't work. It had to be in both places. Is that the expected behavior of grails?


Solution

  • That's not the expected behaviour. Get the latest Grails version and use jQuery as described in the documentation: http://grails.org/doc/latest/guide/theWebLayer.html#6.7%20Ajax

    Prototype can also be used, but jQuery is preferred since Grails 2.0.

    A simple <g:javascript library="jquery" /> should then do the trick.

    BTW: V2.0 is pretty new. Most tutorials and books are still refering to 1.3.7 . So if you run into problems, check the really fine manual. But don't get me wrong: most of the books and tutorials are still great and valuable - you should just consult the manual, too!