【发布时间】:2012-03-02 16:05:19
【问题描述】:
我已经为我的 CakePHP 设置了 REST,但我遇到了一个小问题。当调用 GET 方法时,比如我的控制器上的视图或索引,甚至是 GET 的自定义方法,我都可以毫无问题地得到答案。但是,当我尝试执行诸如 add 之类的 POST 操作时,即使我可以看到它正确到达并执行(保存到数据库),我也没有从操作中得到任何输出。
我已经正确设置了 JSON 和 XML 输出的布局文件以及每种输出类型的路由和视图。
编辑:
beforeFilterAppController中的相关代码:
if ( $this->RequestHandler->isAjax() ) {
$this->layout = 'ajax';
$this->autoRender = false;
} elseif ($this->RequestHandler->isXml()) {
$this->layout = 'default';
$this->RequestHandler->respondAs('xml');
$this->RequestHandler->renderAs($this, 'xml');
} elseif ($this->RequestHandler->ext == 'json') {
$this->layout = 'default';
$this->RequestHandler->respondAs('json');
$this->RequestHandler->renderAs($this, 'json');
} elseif ($this->RequestHandler->accepts('html')) {
$this->layout = 'frontend';
}
路线中的代码:
Router::mapResources('fonykers');
Router::mapResources('voicenotes');
Router::parseExtensions();
我在FonykersController 中的add 方法中的相关代码:
$response = array('ok' => true, 'title' => 'Thank you for registering', 'msg' => 'A confirmation email has been sent to the provided email address, please click on the link in the email to complete the registration process');
if ($this->RequestHandler->isXml()) {
$this->set(compact('response'));
} else if ($this->RequestHandler->ext == 'json') {
pr('This prints');
$this->set(compact('response')); //View not being outputted
pr('This also prints');
} else {
echo json_encode($response);
}
我在/views/fonykers/json的看法
<?php echo json_encode(array('response' => $response)); ?>
我的 json 布局文件:
<?php
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header('Content-Type: application/json');
header("X-JSON: ".$content_for_layout);
echo $content_for_layout;
?>
【问题讨论】:
-
不看相关代码就无话可说。
-
用相关代码更新了我的问题
标签: json cakephp rest cakephp-1.3