AJAX blocked by SecurityComponent

I use jquery cookie plugin to read the CSRF cookie that is stored in your browser and encapsulate that in a variable in my beforeSend function…

beforeSend: function(request){
                    var csrf = $.cookie('csrfToken');
                    request.setRequestHeader('X-CSRF-Token', csrf);
                }
url:your_url;

After that I send the info to my url un my ajax request and you are done.

Right after in the same ajax call, on your success object you’ll read the response from your controller. That response will be easier to read using JSON.

success:function(data){
    var yourResponse = $.parseJSON(data);
}

and voila, that is in your script, now in your controller you can try this…

1 catch your parameters if any:

public function yourAjaxFriend( $data = null){
    if($this->request->is('ajax','post')){
        $this->response->disableCache();
        $this->autoRender = false;
        $this->RequestHandler->config('inputTypeMap.json', ['json_decode', true]);

       // do all your logic here and set your variables to bring back to the view
       // for example
       $varBack = my_result;
       $this->set(compact($varBack));
       echo json_encode(compact('varBack'));
       exit();
    }
}

Remember to load the cookie before your run your javascript code.

Hope this helps and happy coding !