Search code examples
javascriptjqueryajaxgrails

Get params from Ajax Call in controller


my ajax call is passing following data:

var quoteParams ={check:true,startDate:date}
 $.ajax({
        type: "POST",
        url: "check/save",
        data: {orderForm : gatherOrderDetails(), quoteForm : quoteParams } ,      // GatherORderDetails will get all the form input values from a form


        success: function(msg) {
            alert(msg.d);
        },
        error: function(msg) {
        alert('error');
        }

    });

So in my controller i want to get the 'orderform' and ' quoteForm' as object of class.the class are Class orderssForm and Class quotesForm

def save(ordersForm orderForm,quotesForm quoteForm){
//now i need to save all the details obtained from 'orderform' and 'quoteform' after procerssing them in database.

}

But i get error that the params is invalid.


Solution

  • Just define your save action like this:

    def save() {
        // do stuff with orderForm
        params.orderForm
    
        // do stuff with quoteForm
        params.quoteForm 
    }
    

    Also take a look at data binding which can massively simplify the persistence side of things.

    Not sure what your javascript gatherOrderDetails() function is doing but if you just need to submit all form elements you can use serialize e.g.

    var data = $('form').serialize();