Will
Will

Reputation: 482

Is it possible to render json data into a javascript variable while making an ajax call in grails?

Is it possible to render json data into a javascript variable while making an ajax call in grails?

I am using the submitToRemote inorder to make an ajax call from my grails view to an action in my grails controller. The action returns a json variable/value. I need to to assign this value to a javascript variable for further usage on my web page. Is this possible to achieve? Any leads will be helpful.

Upvotes: 0

Views: 700

Answers (2)

user800014
user800014

Reputation:

submitToRemote have the onSuccess option that you can use to retrieve the json data. From the docs:

onSuccess (optional) - The JavaScript function to call if successful

An example of how doing it can be seen in this blog post.

Upvotes: 1

aiolos
aiolos

Reputation: 4697

You could use the onSuccess callback of submitToRemote to read the result of your request and pass them into a javascript variable.

<script type="text/javascript">
    function passResult(result) {
        yourVariable = result.responseText      
    }
</script>

<g:form action="show">
    Login: <input name="login" type="text" />
    <g:submitToRemote update="updateMe" onSuccess="passResult(result)"/>
</g:form>

<div id="updateMe">this div will be updated with the form submit response</div>

The code above is untested but should work.

Upvotes: 1

Related Questions