1

首先,对不起我的英语不好,我是法国人。

这里的问题:

我有一个看起来像这样的模板:

<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>

这里是重新加载片段页面的 GSP:

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

这里生成的javascript(通过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>

这里是执行此操作的控制器代码:

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 ?!"
}

日志“一切都好”是唯一呈现的。但是每次,整个页面都会在响应中重新加载,我真的不明白为什么。

提前致谢,

斯奈特

4

4 回答 4

1

每当我发生这种情况时,都是因为我的 GSP 上没有这个:

<g:javascript library="jquery" />
于 2012-02-23T19:38:41.230 回答
1

发生这种情况是因为您没有将任何 JavaScript 库作为资源附加到您的页面。所以浏览器无法调用指定的函数,只是跟随链接。要纠正这种情况,请在标题中添加:

<g:javascript library="jquery"/>
<r:layoutResources/>
于 2012-02-27T09:55:29.033 回答
0
<g:javascript library="prototype" />
于 2012-02-28T09:26:11.880 回答
0

我终于找到了答案。

这是因为 js 执行:

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

设计师使用的这段代码用于演示,并重新加载页面。评论此代码解决了问题。

不容易找到!

抱歉今天才回答,谢谢你的回答!

于 2012-03-21T15:30:02.367 回答