cakephp Ajax request handling Basic CakePHP 2.x example


Controller: In the Controller you have to add the RequestHandler component. This Enables CakePHP to automatically detect Ajax requests(see: for more info):

class YourController extends AppController {
    public $components = array('RequestHandler');

    public function ajaxCall() {
            // some code that should be executed
            // ...
            // variables you want to return
            $this->set(compact('firstVariable', 'secondVariable'));
            $this->set('_serialize', array('firstVariable', secondVariable))

View Code (using jQuery):

    type: 'POST',
    url: '/yourController/ajaxCall',
    success: function (result) {
        // result is a JSON array of the returned Variables
    error: function (result){