Ajax和Grails的傻瓜

I am a rookie Grails user and I am completely new to AJAX. I am not exactly grasping the concept of AJAX, and the material online is fragmented.

From my understanding, in grails if I wanted to execute a method in one of my controllers when a part of my HTML doc loads I could simply use something along the lines of

<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...>
  1. How is the response from the call to the hypothetical foo returned and how can I access it in a js function?
  2. Can I return an object from a class I created from my hypothetical foo action?

On the return of the foo action you can put simple html as text or render some objects that can be used in the view.

Here you have all the info about the Controller "render"

http://grails.org/doc/latest/ref/Controllers/render.html

You can have a that will be update with that data and work there with it. Then you can access to the Html and data inside that "foo" div with javascript like you usually do.

For example:

Controller.groovy

// renders text to response
render '<div id="bar" onclick="alert($('bar').val())>some text</div>'

View.gsp

//Makes the call and updates foo
<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...>
<div id="foo" name="foo"></div>

Output

<div onload="theAjaxJavascriptFunctionThatGrailsWillInject" ...>
<div id="foo" name="foo">
    <div id="bar" onclick="alert($('bar').val())">some text</div>
</div>

I you return some object from the Controller.grooy then you have to treat it like this in you View.gsp

//Makes the call and updates foo
<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...>
<div id="foo" name="foo">
    ${myObject.theValueIWant}
</div>

I added a javascript alert but you can do it the way you like, there are lots of ways to do it.

Hope it helps :)