Grails RemoteLink:响应中加载整个页面

发布于 2025-01-08 08:16:11 字数 2462 浏览 5 评论 0原文

首先,抱歉我的英语不好,我是法国人。

问题在这里:

我有一个如下所示的模板:

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

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

预先感谢,

斯尼特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喵星人汪星人 2025-01-15 08:16:11

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

<g:javascript library="jquery" />

Whenever I have had this happen it was because I didn't have this on my GSP:

<g:javascript library="jquery" />
ˉ厌 2025-01-15 08:16:11

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

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

This is happening because you didn't attach any JavaScript library as a resource to your page. So the browser fails to call the specified function and just follows the link.
To correct the situation add to your header:

<g:javascript library="jquery"/>
<r:layoutResources/>
懒猫 2025-01-15 08:16:11
<g:javascript library="prototype" />
<g:javascript library="prototype" />
往事随风而去 2025-01-15 08:16:11

我终于找到了答案。

这是因为js执行:

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

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

不容易找到!

抱歉今天才回复,谢谢您的回复!

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文