【问题标题】:Render Grails template depending on JS variable根据 JS 变量渲染 Grails 模板
【发布时间】:2013-03-21 15:01:37
【问题描述】:

情况是:我有一个名为 status 的 javascript bool 变量。 如果是真的,我想渲染一个 Grails 模板。 如果它是假的,我想渲染另一个 Grails 模板。 我的代码:

HTML/JS

<div id="content">
<g:javascript>
    $( function(){
        if( status==true ) {
            $.ajax({
                url: '/tool/home/renderSplash',
                type: 'POST',
                failure: function(){alert("Failed loading content frame"})
            }
        else{
            $.ajax({
                url: '/tool/home/renderContent',
                type: 'POST',
                failure: function(){alert("Failed loading content frame")}
            })
        }
    } )
</g:javascript>
</div>

Grails:

class HomeController {

  def index() {}

  def renderSplash() {
      render( template: "splash" )
  }

  def renderContent() {
      render( template: "content" )
  }
}

我可以看到,POST 包含正确的模板数据,但它没有显示在屏幕上。

我的做法是否正确?

【问题讨论】:

  • 我没有看到任何可以处理响应并将内容放入 dom 的代码。你如何处理 ajax 响应?

标签: javascript jquery ajax grails


【解决方案1】:

将成功处理程序添加到呈现结果的 AJAX 调用。您需要一个占位符元素来呈现它。示例:

$.ajax({
       url: '/tool/home/renderContent',
       type: 'POST',
       failure: function(){alert("Failed loading content frame")},
       success: function(response) { $('#response').html(response); }
   })

...
<div id='response'></div>

【讨论】:

    猜你喜欢
    • 2010-11-30
    • 1970-01-01
    • 2016-01-07
    • 2016-05-22
    • 2011-03-14
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多