How to trigger POST request using JavaScript in Grails framework

I’m working on a Grails application and need help with sending POST requests through JavaScript. My goal is to submit a POST request whenever a user selects a different option from a dropdown menu.

I’ve been experimenting with jQuery’s $.post() function. While it does successfully trigger my controller method, I’m having trouble getting the webpage to update with the new data that comes back. The page stays the same and doesn’t show any changes. How can I make the page refresh or update with the response data?

The request doesn’t have to be asynchronous - I’m fine with a regular synchronous POST if that works better. I’m open to solutions beyond jQuery as well.

My current JavaScript code:

<script type="text/javascript">
  $(document).ready(function() {
     $("#companySelect").change(function() {
       $.post("/myApp/userProfile/companyChanged", {companyId: this.value});
      });
  });
</script>

Any suggestions on how to properly handle the response and update the page content would be really helpful. Thanks!

u totally need to manage the server’s response better. try adding .then(response => $('#someDiv').html(response.responseText)) after that $.post call. if ya just want a full refresh, you could go with location.reload() too.

You’re not handling the response from your POST request. Your code sends the data but completely ignores what comes back.

Add this:

$.post("/myApp/userProfile/companyChanged", {companyId: this.value})
  .done(function(response) {
    // Update specific div with new content
    $('#contentDiv').html(response);
    // OR reload entire page
    // window.location.reload();
  });

Honestly though, managing all these AJAX calls and DOM updates gets messy fast. Been there with complex Grails apps.

Now I just automate the whole thing with Latenode. Set up workflows that handle POST requests, process data, and update your frontend automatically. No more wrestling with JavaScript callbacks.

Latenode connects directly to Grails and triggers actions based on user interactions. Way cleaner than manual AJAX management.

Check it out: https://latenode.com

u should add a success callback to your $.post(). something like .done(function(data) { location.reload(); }) will work to refresh the page. but honestly, its better to just update specific parts of the page with the response for a smoother feel, ya know?

Had this exact problem building a project management dashboard - team dropdowns weren’t updating task lists.

Your POST fires but you’re not handling the response. The controller sends back updated HTML or data, then JavaScript just dumps it.

Skip the JavaScript callback mess. Set up a workflow that catches dropdown changes, runs the POST, and auto-updates your page sections. No manual coding needed.

I do all my Grails forms this way now. Workflow grabs user selections, hits controller endpoints, pushes updates to page elements. Done with debugging AJAX callbacks.

Way cleaner than writing custom JavaScript for every dropdown. Scales better when you add more interactive stuff too.

Check it out: https://latenode.com

Your JavaScript fires the POST but doesn’t wait for the server response. I hit this same issue last year on a user dashboard where dropdown changes had to update multiple sections.

Your controller’s probably returning updated HTML or JSON, but you’re not capturing it so nothing happens. Handle the response like this:

$.post("/myApp/userProfile/companyChanged", {companyId: this.value})
  .done(function(responseData) {
    $("#mainContent").replaceWith(responseData);
  })
  .fail(function() {
    alert("Something went wrong");
  });

Make sure your Grails controller renders the right view fragment or JSON. Want a full page refresh instead? Just use window.location.href = window.location.href; in the done callback. The synchronous approach would work too, but it freezes the browser during requests - terrible UX.

You’re sending the POST request but not handling the response. Your controller needs to return the updated content, and your JavaScript needs to process what comes back.

Make sure your Grails controller renders the updated content or redirects properly. Then update your JavaScript:

$.post("/myApp/userProfile/companyChanged", {companyId: this.value})
  .success(function(data) {
    $("#targetElement").html(data);
  })
  .error(function() {
    console.log("Request failed");
  });

Honestly, I’d recommend using Grails remoteFunction instead. It handles responses automatically and updates page elements without manual DOM work. The g:remoteFunction tag plays nice with Grails controllers and beats raw jQuery AJAX calls every time.