views:

291

answers:

2

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});
      });
  });
+4  A: 

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?

Nick
I actually don't have a form element on my page... so I guess using a POST was an incorrect assumption on my part. Initiating a GET would suffice.... but I'll put a form around my drop-down box and try what you suggested.
anschoewe
If all you want is a GET, you can just update the `window.location` property.
Hank Gay
A: 

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>
Ed.T