Cake Php 3.6-Ajax调用发送空数据

时间:2019-03-22 12:34:10

标签: ajax cakephp cakephp-3.0

在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请求有效载荷

enter image description here

0 个答案:

没有答案