简体   繁体   English

将数据从.gsp发送到控制器

[英]Send data from .gsp to controller

I need send data from text input in .gsp page to a controller using js or ajax. 我需要使用js或ajax将数据从.gsp页面中的文本输入发送到控制器。 My code in gsp page is 我在gsp页面中的代码是

 <input placeholder="<g:message code="form.search.label"/>" id="q" name="q" type="text" onkeypress=validar(event);>
 <button class="button"><span class="mif-search"></span></button>

The js function is: js函数是:

 <script type="text/javascript">
function validar(event){
    var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    var message = $('#q').val();
    if (theCode == 13){           

        <g:remoteFunction controller="search" action="search" params="message" update="pageid"/>          
}
}

This code work fine is I wich refresh my page, but I need send the data in the input text to my controller. 只要刷新页面,此代码就可以正常工作,但是我需要将输入文本中的数据发送到控制器。 I surf in web but I cant resolve this problem. 我在网上冲浪,但无法解决此问题。 What I need to do for this? 我需要为此做什么? Thanks in advance. 提前致谢。

I strongly recommend use jQuery.ajax(); 我强烈建议使用jQuery.ajax(); instead of g:remoteFunction because at the end of the day. 而不是g:remoteFunction因为在一天结束时。 The g:remoteFuncion it would be rendered to jQuery.ajax(); g:remoteFuncion呈现给jQuery.ajax(); . For example, you have this: 例如,您有:

<g:remoteFunction controller="search" action="search" params="message" update="pageid"/>

This will be: 这将是:

jQuery.ajax({type:'POST', url:'/YOURGRAILSAPPNAME/search/search',success:function(data,textStatus){jQuery('#pageid').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){}});

You can test looking the HTML in your webpage. 您可以测试网页中的HTML。

But if you really want to use the g:remoteFunction . 但是,如果您真的想使用g:remoteFunction You can use this 你可以用这个

var message = $('#q').val();
if (theCode == 13)
    <g:remoteFunction controller="search" action="search" params="'message='+message" update="pageid"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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