'_Token.fields' was not found in request data

Hi all, I have a problem with an ajax call in my cakePhp 4.4.17 project. In my template file I launch a call to load a jquery datatable but I always get:
code :400
message :“‘_Token.fields’ was not found in request data.”

My request code is as follows:

        echo ' var '.$randomID.' = $(\'#'.$randomID.'\').DataTable({';
            echo '"ajax": { ';
            echo '          "type": "POST",';
            echo '          "url": "/ajaxWarehouseOrders",';
            echo '          "headers": {';
            echo '                       "X-CSRF-Token":  $("meta[name=\'myToken\']").attr("content")';
            echo '                     },';
            echo '          "data": {';
            echo '                   "status": "'. $statusName . '" ,';
            echo '                   "status_id": "'. $statusId . '",';
            echo '                   "_Token": null';
            echo '                   }';
            echo '        }, ';
            echo '"columnDefs": [{"type": \'alberghiero\', "targets":"__order__"}],';
            echo '"ordering": true,';
            echo '"pageLength": 10';
        echo '});';"

I haven’t a form, I just need only load data from controller.

Thx all

Depending on which component you use you are better of disabling the Security (and or FormProtection) Component for that specific action which requires you to use AJAX



Thx Kevin, now I have a new 2 new problem:

  1. my data, sending via POST, i don’t know why, but if i do “$post = $this->request->getData();” is empty…
  2. My respnse is

message: Illegal offset type in isset or empty
url: /ajax ec
code 500
file : …\vendor\cakephp\cakephp\src\Datasource\Paging\NumericPaginator.php
line: 480

can you help me?

Are you posting data to an action which paginates a query? This seems rather weird…

Why it’s weird?
I amnew programmer in cakephp i think i wrong some step, I try to explain:

My company gave me an exstisting project in cakephp, and the problem was the very slow loading of the data. Looking through the code I saw that it loaded all data from controllers without using paginators. So my intention is to introduce this feature. But things get complicated, because the list of orders (this is what we are talking about) must be filtered for various states confirmed, unconfirmed, etc… and for each state I must have different lists to load in different datatables. All this is currently done through the index controller which then sends to the index template which loads, for each list, a different datatable, which is loaded in a special tab.
So I’m trying to introduce the paginate in this structure, how can I do it? my idea was to load the datatables via an ajax call that requires data only from the displayed list and therefore I need to pass the various parameters in post, or are there better solutions? Thank you

Pagination parameters are GET parameters, not POST parameters.

GET parameters are retrieved from the request inside a controller via


But POST data (as you already know) is retrieved via


The built in $this->paginate($query) method inside the controller expects your request to contain GET parameters to correctly determine which page it is currently on.
See https://github.com/cakephp/cakephp/blob/4.x/src/Controller/Controller.php#L959

I personally would handle the adding/editing/saving of entries via its own AJAX call to its own controller action (and therefore own URL) which only returns minimal data if the action was successful or not.

The paginating, filtering und searching part should be handled by a different, GET based controller action