How to use AJAX in CakePHP 3.5?

in AppController.php add a method

    public function json($data){
        $this->response->type('json');
        $this->response->body(json_encode($data));
        return $this->response;
    }

and in your ***Controller.php or any other controller

public function simpleAction() {
  if (this->request->is('ajax') && this->request->is('post') ){
    $res = [
        'data' => [
             /* your data */
         ]
    ];
   return $this->json($res);
  }
}
3 Likes