我正在努力使用该<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
with 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 Tool 中看到的只是以下响应:
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
}