Javascript Postback in Grails
-
03-07-2019 - |
Question
Does anyone know how to initiate a POST request in a Grails applications using javascript. Specifically, I would like to be able to POST when a the selected item in a drop-down box is changed.
I've tried using jQuery and the $.post() method. It successfully calls my controller action, but I'm not sure how to get the page to refresh with the response contents. The screen is not updated. Any ideas? This does not need to be asynchronous.
I'm not tied to using jQuery, I'm just trying to figure out how to do a POST from a javascript.
Andrew
My client-side javascript
<script type="text/javascript" language="javascript">
$(document).ready( function() {
$("#ownerId").change(function() {
$.post("/holidayCards/clientContact/ownerSelected", {ownerId: this.value});
});
});
Solution
Find the form object in the DOM you are looking for and cal .submit() on it. Do you have more than one form or multiples on your page?
OTHER TIPS
You mention it is calling your controller action so it is getting information back to the page that is the issue, right?
Try something like this:
def ajaxRandom = {
def randomQuote = quoteService.getRandomQuote()
response.outputStream << "<q>${randomQuote.content}</q>"
}
All your gsp page needs is:
<q>${quote.content}</q>