allthenutsandbolts
allthenutsandbolts

Reputation: 1523

Ajax call not working in grails

I have the following code in my gsp page

$j(document).ready(function() {
        $j("#searchButton").click(function(event){

           var form = $j('#federatedSearchForm');
           $j.ajax({
                       type:'POST',
                       url:'/tabulae/federatedSearch/ajaxGetSearchResults',
                       data:form.serialize(),
                       cache:false,
                       error:function(request,status,error) {
                           alert(status)
                       },
                       success:function(data,status,xhr) {
                           alert(data);
                         },
                       complete:function(xhr,status) {
                       }

               });

           });
   });

I have the folllowing code in my controller

def ajaxGetSearchResults = {

   log.debug "params:" + params

   def returnResults = federatedSearchService.search(params)


   render returnResults as JSON

}

What I'm seeing is that before the controller completes the request. The success method is called on the client side. Is there something I'm doing wrong or in-correct ?

Upvotes: 0

Views: 621

Answers (2)

Rob Hruska
Rob Hruska

Reputation: 120286

Returning false from your click handler will prevent the browser's default action (likely a form submission):

$j(document).ready(function() {
    $j("#searchButton").click(function(event){
        ...

        return false;
    });
});

Upvotes: 1

allthenutsandbolts
allthenutsandbolts

Reputation: 1523

Just had to use

event.preventDefault() before the ajax call

Upvotes: 0

Related Questions