【问题标题】:Grails remoteLink : Entire page loaded in responseGrails remoteLink:响应加载整个页面
【发布时间】:2012-03-13 17:51:14
【问题描述】:

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

这里的问题:

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

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

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

提前致谢,

斯奈特

【问题讨论】:

  • 确切的行为是什么:页面是否重新加载(如果是,则检查页面上是否有 javascript 错误(萤火虫)),如果页面呈现到 div 中,请仔细检查渲染线。也许您必须添加layout: 或一些&lt;r:layoutResources/&gt;。你在使用 grails 2 吗?

标签: ajax grails gsp grails-controller


【解决方案1】:

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

<g:javascript library="jquery" />

【讨论】:

    【解决方案2】:

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

    <g:javascript library="jquery"/>
    <r:layoutResources/>
    

    【讨论】:

      【解决方案3】:
      <g:javascript library="prototype" />
      

      【讨论】:

        【解决方案4】:

        我终于找到了答案。

        这是因为一个js执行:

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

        此代码由设计师使用,用于演示并重新加载页面。 注释此代码解决了问题。

        不容易找到!

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

        【讨论】:

          猜你喜欢
          • 2012-08-25
          • 1970-01-01
          • 1970-01-01
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          • 2014-11-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多