我如何将RemoteFunction值传递给Grails中的Java Script

时间:2015-07-02 07:43:10

标签: javascript jquery grails

任何人都可以帮我解决问题。为什么我的javascript文件没有从控制器获取值。 这是我的javacript代码

$("#email").click(function() {
            alert("dhukse");
            ${remoteFunction(
                    controller: 'login', 
                    action:'checkNumber',
                    update:'myDiv',
                    params:'\'number=\'+$(\'#phone\').val()'
                    )};
            alert(${number});

            });  

这是我的控制器功能

    def checkNumber(){
            def number=params.number
            println params.number
            def key
            def user=Login.findByPhone(params.number)
            println user
            if(user){
                key=1
            }
            else{
                key=0
            }
            println key
            //println user.phone
            [number:key]
}

1 个答案:

答案 0 :(得分:0)

file.gsp:

<div id="mydiv"></div>

   <g:javascript>
        $("#email").click(function() {
                    var phone = $("#phone").val();
                    ${remoteFunction(
                            controller: 'login', 
                            action:'checkNumber',
                            update:'mydiv',
                            params:[number:phone]
                            )};


                    }); 
    </g:javascript>

<强> _response.gsp:

<g:if test="${user}">   |  <g:if test="${user}">
    <p>key: 1 </p>      |      <p>phone: ${user?.number} </p>
</g:if>                 |  </g:if>
<g:else>                |  <g:else>
    <p>key: 0 </p>      |      <p>not user </p>
</g:else>               |  </g:else> 

<强>控制器:

class LoginController {
     def checkNumber(PhoneCommand command){
                println command?.number
                def user=Login.findByPhone(command?.number)

                render template: "response", model: [user:user]
    }
}

    @grails.validation.Validateable
    class PhoneCommand {
        String number
        static constraints = {
            number(blank: true, matches: "\d+")
        }
    }