在Cake PHP 3.6中,我的Ajax调用正在向控制器发送空数据。 我已经包含了$ this-> loadComponent('RequestHandler');在AppController中。 数据库不保存数据,响应始终是一个空数组。 无法弄清楚为什么会这样。
Ajax呼叫:
var clubeName = 'MyClub';
$.ajax({
method:'POST',
async: true,
cache: false,
dataType: "json",
url: '/Ajax/add',
data: {'club' : clubeName},
error: function(response){
console.log('erro');
},
success: function(response)
{
console.log(response);
}
});
我的控制器操作:
public function add()
{
$ajax = $this->Ajax->newEntity();
if ($this->request->is('ajax')) {
$ajax = $this->Ajax->patchEntity($ajax, $this->request->data());
if ($this->Ajax->save($ajax)) {
$this->Flash->success(__('The ajax has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The ajax could not be saved. Please, try again.'));
}
$this->set(compact('ajax'));
$this->response->body(json_encode($ajax));
return $this->response;
}
编辑 AJAX请求有效载荷