【发布时间】:2015-03-17 17:09:09
【问题描述】:
我尝试将一些 json 传递给 cakePHP 2.5 中的控制器并再次返回它以确保一切正常。
但是我没有收到任何回复内容。只成功了200。通过阅读文档,我的印象是,如果我传递了一些 json,那么 responseHandler 将返回 json 作为响应。
不知道我错过了什么。
正在传递的数据
var neworderSer = $(this).sortable("serialize");
给了
item[]=4&item[]=3&item[]=6&item[]=5&item[]=7
appController.php
public $components = array(
'DebugKit.Toolbar',
'Search.Prg',
'Session',
'Auth',
'Session',
'RequestHandler'
);
index.ctp
$.ajax({
url: "/btstadmin/pages/reorder",
type: "post",
dataType:"json",
data: neworderSer,
success: function(feedback) {
notify('Reordered pages');
},
error: function(e) {
notify('Reordered pages failed', {
status: 'error'
});
}
});
PagesController.php
public function reorder() {
$this->request->onlyAllow('ajax');
$data = $this->request->data;
$this->autoRender = false;
$this->set('_serialize', 'data');
}
更新: 我现在已将以下内容添加到 routes.php
Router::parseExtensions('json', 'xml');
我已将控制器更新为
$data = $this->request->data;
$this->set("status", "OK");
$this->set("message", "You are good");
$this->set("content", $data);
$this->set("_serialize", array("status", "message", "content"));
现在一切正常。
【问题讨论】:
-
所以你正在使用(或者你想要)JSON view,对吧?
-
是的,我想使用 JSON 视图,通过阅读文档,cakephp 是否没有选择所需的适当视图?