Skip to content
Advertisement

CRUD table with Ajax + Datatables does not work

I’m creating a CRUD table with Ajax + Datatables, but for any reason, my code does not work.

I can get my json response with my data from my database, but I don’t know why, it can’t be printed on my table.

Im using 2 files:

1.- main.php:

JavaScript

2.- crud.php:

JavaScript

Just to clarify some things … I’m working under a custom CMS, that’s why I don’t need to import bootstrap, jquery or Datatables, because is already imported.

Also, I’m loading header, content and footer from a custom function, so … Just to clatify that.

So, this is my table (I can show the data):

enter image description here

And this is my json response I get when I refresh my page and my Ajax call is thrown:

enter image description here

I can’t show my data completelly, but I get all data from my database, the information what I need, so Ajax is working properly.

I need to show what I get from my response on my table. How can do that? My code is not working 🙁

Thanks in advance, guys

Advertisement

Answer

Remove "dataSrc": "" – you should not need to use the dataSrc option at all, because it looks as if your JSON row data is already in a top-level named array: { ... "data": [...] ... }.

By default, the name used by DataTables for the row iteration entry point is data. So, not using dataSrc at all is the same as using "dataSrc": "data".

If you use "dataSrc": "", that is telling DataTables that your row array is in a JSON structure like this – an array of arrays:

JavaScript

Or like this – an array of objects:

JavaScript

And in both of the above cases, that outer array is the top-level of the JSON string, and it does not have a name, and there is no containing {...}.

The dataSrc option is documented here, for reference.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement