【发布时间】:2012-10-18 20:07:17
【问题描述】:
我正在调用一个控制器函数:
$.get("http://localhost/universityapp/courses/listnames", function(data){
alert("Data Loaded: " + data);
});
在我的控制器中:
public function listnames() {
$data = Array(
"name" => "Sergio",
"age" => 23
);
$this->set('test', $data);
$this->render('/Elements/ajaxreturn'); // This View is declared at /Elements/ajaxreturn.ctp
}
在那个视图中:
<?php echo json_encode($asdf); ?>
但是,Action 会返回整个页面,包括布局内容(页眉、页脚、导航)。
我在这里缺少什么?如何只返回没有布局内容的 JSON 数据?
【问题讨论】:
-
设置 $this->layout = null ;在列表名称中
-
@MoyedAnsari:如果你把它写成你的答案,我会接受它作为解决方案。
-
已发布答案希望这对你有用
-
一定要
$this->response->type('json');????
标签: php json cakephp controller cakephp-2.2