【发布时间】:2012-07-26 22:46:58
【问题描述】:
我正在使用带有 grails remoteFunction 标记的 jquery 库在鼠标悬停时进行 AJAX 调用。从我写的 println 中我知道正在调用指定的操作,但永远不会触发 onSuccess 函数。我检查了萤火虫,我收到了 404 错误。我一般是 AJAX 和 JS 的新手,所以我现在可能忽略了一些非常明显的东西。这是我的代码 sn-p。
gsp:
<script type="text/javascript">
function change()
{
document.getElementById('changer').src='${resource(dir: "images/images", file: "heart_red.png")}';
}
function onSuccess(data){
alert("Has hearted:");
}
<img class="user_profile_pic" src="${user.profilePicture}" onmouseover="${remoteFunction(controller:'user', action: 'hasHearted', onSuccess: 'onSuccess(data)', params:[userID: user.id])}"/>
时髦的:
def hasHearted = {
println "Recieved user ID: $params.userID"
if(some condition...){
[hasHearted: true] as JSON
}
else{
[hasHearted: false] as JSON
}
}
【问题讨论】:
-
我要做的第一件事是使用 Firebug 之类的东西来确保您的 hasHearted 操作正确返回。如果未调用 onSuccess,则可能是它不成功,即使它已到达服务器。您必须验证往返行程。不只是一种方式。
-
你真的应该把脚本和 HTML 分开..
-
谢谢,萤火虫是个好主意。我收到 404 错误。有什么想法可以发生吗?
-
404?首先要看的是你的控制器名称和动作名称。
标签: javascript jquery ajax grails groovy