2013-03-21 149 views
1

情况是:我有一个名为status的javascript bool变量。 如果这是真的,我想渲染一个Grails模板。 如果它是假的,我想渲染另一个Grails模板。 我的代码:根据JS变量渲染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") 
    } 
} 

我可以看到,该帖子包含了正确的模板数据,但它没有带来到屏幕上。

我在做正确的方法吗?

+0

我没有看到任何将处理响应并将内容放入dom的代码。你如何处理Ajax响应? – 2013-03-21 15:12:49

回答

4

将成功处理程序添加到呈现结果的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> 
+1

很多东西我还有<(-_-)> – holgrich 2013-03-21 15:46:01