我要AJAX调用结束后显示出一些闪光的消息。我做这样的..
I want to show some flash message after completion of AJAX call. I am doing like this ..
控制器动作 -
def subscribe()
{
def subscribe = new Subscriber()
subscribe.email = params.subscribe
if (subscribe.save())
{
flash.message = "Thanks for your subscribtion"
}
}
查看部分 -
Subscribe :
<g:formRemote onSuccess="document.getElementById('subscribeField').value='';" url="[controller: 'TekEvent', action: 'subscribe']" update="confirm" name="updateForm">
<g:textField name="subscribe" placeholder="Enter your Email" id="subscribeField" />
<g:submitButton name="Submit" />
</g:formRemote >
<div id="confirm">
<g:if test="${flash.message}">
<div class="message" style="display: block">${flash.message}</div>
</g:if>
</div>
我的AJAX的工作正常,但它没有显示我flash.message。后刷新页面就显示信息。怎么解决呢?
My AJAX working fine but it is not showing me flash.message. After refresh page it displaying message. How to solve it ?
当你使用AJAX页面内容不重新解析,让您的code:
When you use ajax your page content isn't re-parsed, so your code:
<g:if test="${flash.message}">
<div class="message" style="display: block">${flash.message}</div>
</g:if>
将不会再次运行。
will not run again.
所以,我同意@詹姆斯的评论,闪光灯是不是更好的选择给你。
So I agree with @James comment, flash is not the better option to you.
如果你需要更新你的观点,去用JSON。 Grails的已经有一个变换器的,可用于本
If you need to update your view, go with JSON. Grails already have a converter that can be used to this:
if (subscribe.save()) {
render ([message: "Thanks for your subscribtion"] as JSON)
}
和您的看法:
<g:formRemote onSuccess="update(data)" url="[controller: 'TekEvent', action: 'subscribe']" name="updateForm">
<g:textField name="subscribe" placeholder="Enter your Email" id="subscribeField" />
<g:submitButton name="Submit" />
</g:formRemote >
<script type='text/javascript'>
function update(data) {
$('#subscribeField').val('');
$('#confirm').html(data.message);
}
</script>