0

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.

2
  • Checkout the onsuccess param of submitToRemote. Here's a example Commented Jun 19, 2012 at 12:20
  • Sérgio Michels : it works fine...only had to replace the 'e' with 'data'. I think jquery uses 'data'. Please could you put in the answer below so that I can select it as the correct answer? Commented Jun 19, 2012 at 12:59

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this worked for me. Thanks Sérgio! Only thing for some reason, 'e' did not work for me, but 'data' did!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.