我正在使用带有grails remoteFunction标记的jquery库在鼠标悬停事件中进行AJAX调用。从我写的println我知道指定的操作正在被调用,但onSuccess函数从未触发。我检查了萤火虫,并收到404错误。我对AJAX和JS是一般的新手,所以我现在可能会忽略一些非常明显的东西。这是我的代码片段。Grails:AJAX onSuccess方法不触发
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,则可能是因为它未成功,即使它已将其发送到服务器。你必须验证往返。不只是一种方式。 – Gregg 2012-07-28 14:08:02
你应该真的从HTML分离脚本.. – EricG 2012-07-28 15:10:47
谢谢,萤火虫是一个好主意。我收到一个404错误。任何想法可以发生什么? – 2012-07-28 21:09:22