Grails RemoteLink:响应中加载整个页面
首先,抱歉我的英语不好,我是法国人。
问题在这里:
我有一个如下所示的模板:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每当我遇到这种情况时,都是因为我的普惠制上没有这个:
Whenever I have had this happen it was because I didn't have this on my GSP:
发生这种情况是因为您没有将任何 JavaScript 库作为资源附加到您的页面。因此浏览器无法调用指定的函数,而只是跟随链接。
要纠正这种情况,请添加到您的标题中:
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:
我终于找到了答案。
这是因为js执行:
这段代码是设计者使用的,用于演示,并重新加载页面。
评论此代码解决了问题。
不容易找到!
抱歉今天才回复,谢谢您的回复!
I finally found the answer.
It was because of a js execution :
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 !