How to use AJAX in CakePHP 3.5?

Thank sourjya

Had tried with “return json_encode($ now);” but also caused error.

In the end it was like that and it works correctly.

test/src/Controller/AjaxController.php

public function simpleAction() 
{
    if ($this->request->is(array('ajax'))) 
    {
        $now = new Time();

        // the order of these three lines is very important !!!
        $resultJ = json_encode(array('result' => array('now' => $now)));
        $this->response->type('json');
        $this->response->body($resultJ);

        return $this->response;
    }        
}