【发布时间】: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:或一些<r:layoutResources/>。你在使用 grails 2 吗?
标签: ajax grails gsp grails-controller