0

我有这样的事情:

$.ajax({
    type: "POST",
    url: "index/test",
    data: { from: from, to: to }
}).done(function(res) {
    alert(res);
});

控制器的答案是:

public function testAction()
{
    echo 555;
}

但问题是,这会返回带有答案 echo 555 的布局;

我怎样才能拒绝渲染布局而只留下回声答案?

4

2 回答 2

2

在您不想呈现任何视图或布局的操作中,请酌情使用以下代码行:-

//To disable view rendering
$this->_helper->viewRenderer->setNoRender(true);
//To disable layout
$this->_helper->layout->disableLayout();

例如:-

public function testAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
    echo 555;
}

您可能也不想看上下文切换,尤其是ajaxContext。这是一种更“ZF”的做法,会自动禁用布局并设置正确的标题。

于 2012-07-19T12:02:08.117 回答
0

您可以通过在控制器操作方法中添加这行代码来禁用布局

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

 public function testAction()
 {
     $this->_helper->layout->disableLayout();
     echo 555;
 }
于 2012-07-19T12:02:06.707 回答