当我尝试向我的函数发布post请求时,我最终使用上述区域。
我的控制器:
public function app_new($profile_type) {
$this->autoRender = false;
// The default response
$response = array('status' => 'FAIL', 'message' => 'There was a problem.');
// Check the request is POST
if ($this->request->is('post')) {
// Get the current logged in users id
$sesh = $this->Auth->user();
$user_id = (isset($sesh['User']['id'])) ? $sesh['User']['id'] : null;
$response['status'] = 'OK';
$response['message'] = 'New post created successfully.';
}
echo json_encode($response);
}
当我尝试向url发送post请求时,我会得到错误,但是如果我做get请求,我会得到默认响应json,因为我应该。
所以当我用url "http://localhost/app/messages/new/1"发出post请求时,我得到了错误,但对相同url的get请求给出了正确的响应
我相信您使用$this->autoRender = false;
的原因是因为您正在发送ajax请求。
如果是ajax,最好勾选
if($this->request->is('ajax')){
//Do your coding here
}
我也没有看到你在哪里保存你的数据,但你只是返回json_encode
,并注意json_encode
应该返回内部if($this->request->is('ajax'))
块
最后但并非最不重要的一件事,你还需要有:
Router::connect('/app_new', array('controller' => 'yourController', 'action' => 'app_new'));
即使函数没有呈现任何视图,路由也必须在那里