简体   繁体   中英

Grails + jQuery: formRemote onSuccess

I'm struggling with the <g:formRemote/> onSuccess method and how I could call my custom JavaScript function properly.

I have the following formRemote element:

<g:formRemote name="userSearchRemoteForm"
                      url="[controller: 'userSearch', action: 'ajaxFindUser']"
                      onLoading="jQuery('#userSearchSubContainerHeadLoaderImage').show()"
                      onSuccess="createUserTable(data)"
                      onComplete="jQuery('#userSearchSubContainerHeadLoaderImage').hide()">
....</g:formRemote>

in debug mode I see, that the Controller function ajaxFindUser is called and returns a valid JSON object like this (from the 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"
}
}}

my JavaScript function createUserTable looks like this:

function createGuestTable(data) {
console.log(data);
}

all I see in my WebDev Tool is the following response:

POST http://localhost:8080/GrailsTest001/userSearch/ajaxFindUser 404 (Not Found)

and my function is never called. I also tried to call the JavaScript function in onSuccess like this:

onSuccess="createUserTable()"

but I still get the same result...

what am I doing wrong here?


Edit: to clear things out - here's my Controller function:

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
}

oh my! I was facing the problem but didn't recognize it would be a problem at all...

I just changed the last line of my ajaxFindUser function from

return jResult

to

render jResult

with the following onSuccess parameter

onSuccess="createUserTable(data)"

and all is working well!!

sorry for that...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM