How do I pass values from grails back to a javascript function?
I have a feeling that the answer to this is easy but I can't seem to get it yet...
I have a Javascript function that calls an action in a Groovy class via a remot开发者_如何学PythoneFunction. Now, I need to return the value from the action class back to the Javascript function for further processing. It looks something like this:
// sample js function in the gsp file
function setPrev() {
...
var tmp = $remoteFunction(action: 'someaction', params: ...)
...
}
In the controller, the action would look something like this:
def someaction = {
...
def retVal = Somedomain.anobject // 1:m relationship so there's a hasMany = [anobject...] in Somedomain
...
return retVal
}
Now, when it returns to tmp, it's [object object]. Is there a way for me to get, at the very least, [anobject1, anobject2, ..., anobjectn] as the value of tmp? I've even tried json but it's still [object object].
it is quite easy
return retVal as JSON
Just remember to import org.grails.converters.JSON (I think it is)
精彩评论