0

App info:

  • Grails 3.1.8
  • Jquery 2.2.0

I have a Grails form which I would like to POST via AJAX. I am able to do this and the data does persist in the database. Code to post is below:

<g:javascript>

    $('#insertSchool').submit(function () {

        $.ajax({
            type: 'POST',
            url: '/school/saveSchool',
            data: $("#insertSchool").serialize(),
            success: function(savedSchool) {


            }

        });

    });

</g:javascript>

Controller code:

def saveSchool(School newSchool) {

    def theSchool = schoolService.saveSchool(newSchool)

    render theSchool as JSON
}

In the success function I would like to redirect to another page and send a message across saying the school has been saved. I'm new to Jquery so I'm unable to implement this. Any help would be appreciated.

kulsoompatel
  • 220
  • 1
  • 3
  • 18

1 Answers1

0

You can use remote function to redirect another page.

<script>
   $('#insertSchool').submit(function () {
        ${remoteFunction(action: 'xyz', controller:'xyz',method: 'post',
            params: 'variable:value',onSuccess: 'redirectFunction();')};
    }

   function redirectFunction(){
        alert("the school has been saved");  
       // AGAIN USE A REMOTE FUNCTION TO REDIRECT ON ACTION (your gsp page)
   }

 </script>
ajay panchal
  • 145
  • 1
  • 11
  • I'm using Grails 3.1.8 and the remoteFunction tag has been deprecated. See - http://docs.grails.org/3.0.0.M1/ref/Tags/remoteFunction.html – kulsoompatel Aug 09 '16 at 14:11
  • this link may help you->\http://stackoverflow.com/questions/25245689/since-gremotefunction-in-grails-2-4-x-is-deprecated-what-should-i-use-instead – ajay panchal Aug 09 '16 at 14:23