Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery get的cakephp 2.1 json错误404_Jquery_Json_Cakephp - Fatal编程技术网

jquery get的cakephp 2.1 json错误404

jquery get的cakephp 2.1 json错误404,jquery,json,cakephp,Jquery,Json,Cakephp,使用cake 2.1,我试图通过ajax获得json记录列表。找不到缺少的内容: 在routes.php上,添加了: Router::parseExtensions('json'); 我的控制器中的操作: public function getdirs($id = null) { $this->RequestHandler->setContent('json', 'application/json'); $dirArray = $this->Dir->f

使用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扩展名并删除了自动渲染;firebug的响应是:

{"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

请帮忙

非常感谢你

var path = "/dirs/getdirs/101.js";
这不应该吗

var path = "/dirs/getdirs/101.json";
尝试安装firebug,或者如果您使用的是chrome for chrome的开发工具,请按F12。这将使您安装调试http。试试网络标签。您可以检查请求并查看错误的确切位置。这将让您确切地看到发送的内容和接收的内容

此外,不确定您是否发布了部分代码,但是否发布了:

$this->autoRender = false;
您需要手动调用render

$this->render('path/to/view');

我建议创建一个通用视图,您可以在输出json的操作之间共享该视图。

404错误是由于我在控制器顶部定义的权限造成的,例如:

public function isAuthorized($user) {
    $AuthAddRoles = array(0, 1);
    if ($this->request->action === 'add') {
        if (in_array($this->Auth->user('role'), $AuthAddRoles)) {
            return true;
        }
    }
}
我只需要以同样的方式授权ajax操作,它就被解决了


感谢dubvfan87提供的所有好建议。现在json响应开始时附带了代码,在这里解决:

谢谢dubvfan;修复了.json扩展名并删除了自动渲染;Firebug的响应是:“网络错误:404未找到-”。找不到行动的原因是什么。双重检查动作名称;见更新后的帖子。请帮忙。我想你需要$dirArray=$this->Dir->find('all');没有“s”,因为模型的名字是单数的。没错。修正了这个问题,但仍然会出现“NetworkError:404 Not Found-”:-(我会继续搜索thin 404 error.mmmh(已编辑的帖子)。非常感谢!检查您的路由,并确保您的mod_rewrite使用的是正确的.htaccess文件。谢谢dubvfan87;发现问题并作为另一个响应。再次感谢!!