【发布时间】:2014-07-24 06:07:43
【问题描述】:
当我使用时在我的应用程序中
<g:link params=[:] />
参数显示在我不想要的 URL 中。 有没有像我们通过 POST 方法那样发送参数而不显示在 URL 中?
谢谢
【问题讨论】:
当我使用时在我的应用程序中
<g:link params=[:] />
参数显示在我不想要的 URL 中。 有没有像我们通过 POST 方法那样发送参数而不显示在 URL 中?
谢谢
【问题讨论】:
【讨论】:
<g:link class="btn btn-success" id="class.id" onclick='UsingPost();'>SOME LABLE HERE </g:link>
<script type='text/javascript'>
function UsingPost(){
jQuery.ajax({
type:'POST',
data:{"model":${pleaseUseTheModelyouHaveLoaded}"},
url:'${createLink(action: 'save')}',
success:function(data,textStatus){
jQuery('#success').html(data);},
error:function(XMLHttpRequest,textStatus,errorThrown){}
});
}
</script>
【讨论】:
<g:link></g:link> 处理时变为<a href=""></a>,因此传递给 glink 的任何参数将始终在 url 中可用。
如果您不想在 url 中使用 then,则必须使用表单。
【讨论】:
最简单的方法是使用 jquery:
<g:link id="my_link">This is my link</g:link>
<script type='text/javascript'>
$('#my_link').click(function() {
$.post('/url_for_post_request');
});
</script>
Grails 中还有 remoteLink 功能,但它已被弃用,因此最好避免使用它。
另一种选择是使用表单。重要的事实是您可以在某处定义表单并将提交按钮放置在网站上的任何位置 - 您只需要使用g:actionSubmit 的form 属性:
<g:form method="post" action="..." name="form-name">...</g:form>
和g:actionSubmit:
<g:actionSubmit form="form-name" action="..." value="Label"/>
【讨论】:
g:form 的url 属性。参数应作为表单中的隐藏字段传递。
您可以将<g:link params=[:] /> 发送到控制器/动作,然后在动作中进行重定向。
<g:link action="formSubmit" params="[id:3]">send</g:link>
class TestController {
def index() {
}
def formSubmit(Long id) {
// you can use the id param, or add it to flash scope
redirect action: "index"
}
}
所以 id 参数不会显示在结果 url 中。
【讨论】: