CakePHP 2.5.3 状态 500 - 使用 JsonView _serialize 时缺少视图文件

CakePHP 2.5.3 Status 500 - View file is missing when using JsonView _serialize

我正在尝试从我的视图发送一个简单的 AJAX 请求,并且我正在使用 cakePHP 的 JSON 视图来执行此操作,但我无法获取 _serialize 以防止控制器寻找 ctp 文件——我一直收到 "Status 500: view file ... is missing" 错误。我已经阅读了关于 Whosebug 的文档和几个类似的问题,但我看不出我遗漏了什么。

我已将请求处理程序添加到 AppController 中的初始化函数中:

public function initialize() {
    $this->loadComponent('RequestHandler');
}

启用的扩展程序:

Router::parseExtensions('json');
require CAKE . 'Config' . DS . 'routes.php';

我已将组件添加到我的控制器中:

class StudentsController extends AppController {
    public $name = 'Students';
    public $components = array('RequestHandler');

唯一似乎改变它的是当我将以下代码添加到 AppController 的 beforeFilter 方法时——只呈现单词 "null":

$this->RequestHandler->renderAs($this, 'json');
$this->response->type('application/json');
$this->set('_serialize', true);

这是我的控制器方法:

public function set_rating() {
    $this->autoLayout = false;
    $this->autoRender = false;
    $this->response->type('application/json');

    $studentID = (int) $this->request->data['studentID'];
    $rating = (int) $this->request->data['rating'];

    $this->Student->id = $studentID;
    if($this->Student->saveField('rating', $rating)) {
        $this->set('success', 1);
    }
    else{
        $this->set('success', 0);
    }
    $this->set("_serialize", array("success"));
}

和 Ajax 请求:

$.ajax({
    url: '<?php echo $this->webroot . $this->params["controller"]; ?>/set_rating.json',
    type: "POST",
    dataType: "json",
    data:  {studentID: text, rating: value},
    success: function(response) {
        if(response['success'] == 1){
            manageFlashMessage('alert-success', 'Rating saved.');
        }
        else {
            manageFlashMessage('alert-danger', 'Sorry, something went wrong');
        }
    },
    error: function (xhr, status, error) {
       console.log(xhr.status);
       console.log(xhr.responseText);
       console.log(status);
    }
});

我看不到我错过了什么!有什么想法吗?

我在类似的上下文中收到了相同的错误消息;在我的例子中,这是因为我试图调用的方法没有通过 isAuthorized 函数。因此,它被重定向到另一个方法,该方法不打算以 json 格式查看,这就是 "Status 500: view file ... is missing" 错误消息的来源。

我只需要在 isAuthorized 函数中为我的 json 方法添加适当的异常并修复它。

要调试这些类型的错误,可以尝试直接访问 url 而不是通过 ajax 调用,因为这样会显示相应的权限错误消息。