【发布时间】:2012-10-02 04:26:32
【问题描述】:
使用 cake 2.1,我试图通过 ajax 获取 json 记录列表。找不到缺少的东西:
在 routes.php 上,添加:
Router::parseExtensions('json');
我的控制器中的动作:
public function getdirs($id = null) {
$this->RequestHandler->setContent('json', 'application/json');
$dirArray = $this->Dir->find('all');
debug($dirArray);
$this->set('dirArray', $dirArray);
$this->set('_serialize', 'dirArray');
}
在我看来,(编辑父记录 -101- 并需要获取它的子记录),我正在使用这个 jquery:
function updateTable() {
var path = "/dirs/getdirs/101.json";
$.post(path,null,function(data) {
//$.get(path, function(data) {
alert('backFromServer');
alert(data);
});
}
未达到“backfromserver”警报。 另外,我不确定如何测试错误可能在哪里。
一旦我得到 json 数据,将在我的视图上填充一个 div 部分,这部分我已经涵盖,但需要来自服务器的数据。
我真的很感激一些帮助,我已经为此苦苦挣扎了好几天了。
谢谢!!
更新 固定为 .json 扩展名并删除了自动渲染;萤火虫的回应是:
{"code":"404","url":"\/dirs\/getdirs\/101.json","name":"Controller class Controller could not be found."}
"NetworkError: 404 Not Found - http://dev.opm.asc/dirs/getdirs/101.json"
仔细检查了我的操作,它被称为 getdirs 好吧:
public function getdirs($id = null) {
$this->RequestHandler->setContent('json', 'application/json');
$dirArray = $this->Dir->find('all');
debug($dirArray);
$this->set('dirArray', $dirArray);
$this->set('_serialize', 'dirArray');
}
找不到该操作的原因可能是什么?
至于通用视图,在这种情况下,我只想获取数据数组并填充一些 div;
请帮忙。
非常感谢。
【问题讨论】: