我已經爲我的CakePHP設置了REST,並且遇到了一些小問題。當調用一個GET方法時,比如我的控制器上的視圖或索引,甚至是一個GET方法的自定義方法,我都沒有問題得到答案。但是當我嘗試像添加一樣執行POST操作時,即使我可以看到它已正確到達並執行(保存到數據庫),也不會從操作中獲得輸出。CakePHP - POST調用REST API返回null
我已經正確設置了JSON和XML輸出的佈局文件以及每種輸出類型的路由和視圖。
編輯:beforeFilter
相關的代碼在AppController
:
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';
}
守則之路:我add
方法
Router::mapResources('fonykers');
Router::mapResources('voicenotes');
Router::parseExtensions();
相關的代碼在FonykersController
:
$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);
}
我在JSON /views/fonykers/json
<?php echo json_encode(array('response' => $response)); ?>
我的佈局文件視圖:
<?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;
?>
沒有看到相關的代碼就無法說出任何內容。 – JJJ 2012-03-02 17:01:33
用相關代碼更新了我的問題 – 8vius 2012-03-02 17:19:36