2011-06-23 23 views
1

身體各方!在ajax響應中獲得渲染內容

我是zend框架的新手,我正在開發zend框架中基於ajax的選項卡。在點擊一個標籤時,我想要調用一個動作,我想要獲取呈現的內容(在相應視圖中呈現的內容)以發送它作爲響應。

謝謝!

回答

1

查看:

<a href="#" class="tab">Click</a> 
<div id="content"></div> 

JS:

$('.tab').click(function() { 
    $.get('/controller/ajax', function(data) { 
     $('#content').html(data); 
    }); 
}); 

控制器:

public function ajaxAction() 
{ 
    echo 'string'; 
    exit; 
} 

你只需要添加退出;在你的動作中,所以它不會嘗試再次渲染布局。

3

不確定你想要的是什麼,但是你可以禁止在你的動作中渲染布​​局,並且只渲染動作視圖腳本,例如,

public function exampleAction() { 

    if ($this->getRequest()->isXmlHttpRequest()) { 

     $this->_helper->layout->disableLayout(); 

     $this->view->var = 'some var'; 
    } else { 
     throw new Exception('Not an ajax requrests'); 
    } 
} 
+0

謝謝馬爾欽我正好想要這個!非常感謝 – anurodh

+0

這是做這件事的正確方法。不建議使用退出。 –

0
public function yourAction() { 
     // get response and layout 
     $response      = $this->getResponse(); 
     $layout       = $this->_helper->layout(); 

     // rendering action's template  
     $this->render('template'); 

     // setting content, don't remember to echo $this->Layout()->content in the layout script       
     $layout->content     = $response->getBody(); 

     // here you can get your rendered content, and do something with it 
     $renderedContent     = $layout->render('layout'); 

     // you have to clean response otherwise will be automaticaly sent 
     $response->clearBody();           
} 
0

試試這個,也一定要在年底

$this->view->assign(array('name' => 'my name', 'msg' => 'my message')); 
echo $this->view->render('message/display.phtml'); 
exit; 

添加退出,然後在一個.phtml文件只需撥打

<?php echo $this->msg; ?>