简体   繁体   中英

params on remotefunction is not working grails 2.3.4

I am using grails 2.3.4 and according to grails docs there is both <g:remoteFunction> and ${remoteFunction} .I am trying to send values from params to the controller but it is returning null value please help the code is : The gsp page code is :

<div id="test"></div>
    <input type="text" name="myname" id="myname">
<input type="button" name="view" value="view" onclick="myFunction()">

and the javascript function is:

function myFunction(){

        var name=$("#myname").val();        
        <g:remoteFunction controller="test" action="testreport" update="test" params="{n:name}"></g:remoteFunction>

    }

and the controller code is

def testreport(){
        println"test"
println "params value is "+params.n
        println params
    }

and the output is :

test
params value is null
[action:testreport, format:null, controller:test]

I have used g:remoteFunction in many ways, within gsp - you could use:

    <button id=boxbtn onclick="<g:remoteFunction controller="Controller"   
action="action"   params="${[id:inputid, sortby:sortby, order:order, s:s,
  userchoice:'yes', viewtype:'na', offset:offset, max:max]}" update="siteContent"  />">

UPDATE try using this as your myFunction instead of what you had above

function myFunction(){
        var name = document.getElementById('myname');        
        <g:remoteFunction controller="test" action="testreport" update="test" params="${[n:name]}"></g:remoteFunction>

    }

You're messing things up. is executed on the server side when the view gets rendered. Your Javascript variable name will have a value when the browser executes your Javascript on the client side. There is a typo in your code. Instead of

<g:remoteFunction controller="test" action="testreport" update="test" params="{n:name}"></g:remoteFunction>

use

<g:remoteFunction controller="test" action="testreport" update="test" params='\'n=\' + name'></g:remoteFunction>

and if you have more than one param to submit use

<g:remoteFunction controller="test" action="testreport" update="test" params='\'n=\' + name&paramName=\' + varName\''></g:remoteFunction>

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