我正在努力使用<g:formRemote/> onSuccess
方法以及如何正确调用自定义JavaScript
功能。
我有以下formRemote
元素:
<g:formRemote name="userSearchRemoteForm"
url="[controller: 'userSearch', action: 'ajaxFindUser']"
onLoading="jQuery('#userSearchSubContainerHeadLoaderImage').show()"
onSuccess="createUserTable(data)"
onComplete="jQuery('#userSearchSubContainerHeadLoaderImage').hide()">
....</g:formRemote>
在调试模式中我看到Controller
函数ajaxFindUser
被调用并返回这样的有效JSON对象(来自console
prettyPrint == true
):
{"success": {
"376440": {
"phone": "",
"email": "janeDoe@test.com",
"lastname": "Doe",
"firstname": "Jane"
},
"4146": {
"phone": "555-123456789",
"email": "johnDoe@test.com",
"lastname": "Doe",
"firstname": "John"
}
}}
我的JavaScript
函数createUserTable
如下所示:
function createGuestTable(data) {
console.log(data);
}
我在WebDev工具中看到的是以下响应:
POST http://localhost:8080/GrailsTest001/userSearch/ajaxFindUser 404 (Not Found)
我的函数永远不会被调用。我还尝试在JavaScript function
中调用onSuccess
,如下所示:
onSuccess="createUserTable()"
但我仍然得到相同的结果......
我在这里做错了什么?
编辑:清除问题 - 这是我的Controller
功能:
def ajaxFindUser(UserSearchCommand userSearchCommand) {
println("... in ajaxFindUser()")
def result = [:]
if (userSearchCommand.hasErrors()) {
result['fail'] = "Error in search values!"
} else {
println("... creating values for search...")
def user = (User) session.getValue(theUser)
def inputMap = createInputMap(userSearchCommand)
def foundUsers = userSearchService.findUser(user.connectionID, user.language, inputMap)
if (foundUsers == null) {
println("... search returned NULL!")
result = [:]
result['fail'] = userSearchService.getError()
} else {
println("... search returned VALUES!!")
result['success'] = foundUsers
}
}
def jResult = result as JSON
println(".... returning JSON: ${jResult.toString(true)}")
return jResult
}
答案 0 :(得分:3)
我刚从
更改了ajaxFindUser
函数的最后一行
return jResult
到
render jResult
使用以下onSuccess
参数
onSuccess="createUserTable(data)"
一切都运作良好!!
抱歉...