Grails + jQuery:formRemote onSuccess

时间:2012-11-07 09:17:57

标签: jquery forms grails

我正在努力使用<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
}

1 个答案:

答案 0 :(得分:3)

哦,我的! 我正面临着这个问题但却没有意识到这根本就是一个问题......

我刚从

更改了ajaxFindUser函数的最后一行
return jResult

render jResult

使用以下onSuccess参数

onSuccess="createUserTable(data)"

一切都运作良好!!

抱歉...