Search code examples
ajaxgrailsgspgrails-controller

Grails remoteLink : Entire page loaded in response


First, sorry for my bad english, I'm French.

Here the problem :

I have a template which look like this :

<p>
<strong>Whas this answer usefull ?</strong>
<g:remoteLink action="niceAnswer"
              class="btn rate-reply"
              update="comment-${ comment.id }"
              params="${ [commentId: comment.id, nice: true, productId: productId] }">
              Yes
</g:remoteLink>
<g:remoteLink action="niceAnswer"
              class="btn rate-reply"
              update="comment-${ comment.id }"
              params="${ [commentId: comment.id, productId: productId] }">
              No
</g:remoteLink>

Here the GSP for reload the fragment page :

<div class="rating" id="comment-${ comment.id }">
    <g:render template="affRating" model="${ [comment: comment, productId: product.id] }" />
</div>

Here the javascript which is generated (see via firebug) :

<a class="btn rate-reply" action="niceAnswer" onclick="jQuery.ajax({type:'POST',data:{'commentId': '723','nice': 'true','productId': '872'}, url:'/macsf-fronts/action/commentaire/reponse-utile',success:function(data,textStatus){jQuery('#comment-723').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false;" href="/macsf-fronts/action/commentaire/reponse-utile?commentId=723&nice=true&productId=872"> Yes </a>

And here the controller code which execute this action :

def niceAnswer() {
    def comment = DocCommentaireSite.get(params.commentId)
    if (comment && session["comment_${comment.id}"] == null) {
        if (params.nice) {
            comment.nbYesReponseUtile++
        }
        comment.nbReponseUtileVote++
        session["comment_${comment.id}"] = comment.id
        comment.save()
    }

    def product = DocProduitSite.get params.productId
    if (request.xhr) {
        log.debug "niceAnswer : All is good"
        render template: 'affRating', 
               model: [comment: comment, productId: params.productId]
    } else {
        log.debug "niceAnswer : bad behaviour"
        def slugMap = referenceService.getSlugAndParentSlug(product)
        log.info "slugMap : ${slugMap}"
        redirect controller: 'frontRequest', action: 'index', params: [slug: slugMap.slug, parentSlug: slugMap.parentSlug]
    }
        log.debug "niceAnswer : wtf ?!"
}

The log "All is good" is the only which is rendered. But every time, the entire page is reloaded in the response, I really dont get why.

Thanks in advance,

Snite


Solution

  • I finally found the answer.

    It was because of a js execution :

    jQuery('.rating').find('.btn').click(function(e){ 
      e.preventDefault(); 
     jQuery(this).closest('div').load('?zajax=reponse'); 
    }); 
    

    This code, use by designer, was for demo, and reload the page. Comment this code fixed the problem.

    Not easy to find !

    Sorry for answer only today, and thanks for yours !