1

我正在努力使用该<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 对象(来自consolewith 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
}
4

1 回答 1

3

天啊!我正面临着这个问题,但根本没有意识到这将是一个问题......

我刚刚将ajaxFindUser函数的最后一行从

return jResult

render jResult

使用以下onSuccess参数

onSuccess="createUserTable(data)"

一切正常!

对不起……

于 2012-11-07T09:45:36.507 回答