JSON Ajax error when trying to load an empty table from database through MYSQLi query

JSON Ajax error when trying to load an empty table from database through MYSQLi query

Imagchine27Imagchine27 Posts: 7Questions: 2Answers: 1

Hi,

I'm importing data using AJAX and a MySQL queries.

Everything works fine if the table from the database has data. If not, when loading the page, it trows the default JSON error alert.

I'm not using "serverside" parameter.

How could I do to load an empty table from the database and to make datatable recognise it has no data and show the proper message?

Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Are you just returning nothing when the table has no data? If so, that is the cause of the issue as an empty string isn't by itself valid JSON. You need to return your data array as normal, but just have it be empty - e.g. { "data": [] }.

    Allan

  • Imagchine27Imagchine27 Posts: 7Questions: 2Answers: 1

    Yes, when I return [], it don't throw error. But then the message of the table says "loading..." instead of "No data found". Curiously, when I click on a sorteable row, then It recognise the "no data found" message. I need to do this when the table is loaded or when I do a datatable ajax reload.

    How could I achieve this?

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    If you give me a link to the page I can take a look and see why it is showing the loading message.

    Allan

  • Imagchine27Imagchine27 Posts: 7Questions: 2Answers: 1
    edited November 2016

    I am working local.

    But I can paste some links to images:

    Datatable:
    https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/data-table.jpg

    Datatable JS (Part 1)
    https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/data-table-js-1.jpg

    Datatable JS (Part 2)
    https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/data-table-js-2.jpg

    Datatable Ajax PHP
    https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/php-ajax-datatable.jpg

    I pass the datatable parameters using Javascript variables.

    When I click on "Añadir" (Add), delete records or edit some, the Datatable gets ajax reloaded.

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    I don't see where $output is either being defined or used in your PHP, but if DataTables is expecting data in the data property of the returned object, echoing [] isn't going to help - you would need to use { "data": [] } as I suggested above. I would also expect that to be in an if condition to check if it should be output or not.

    Allan

  • Imagchine27Imagchine27 Posts: 7Questions: 2Answers: 1
    edited November 2016

    Yes. I used "{ "data": [] }" but the message is still "Loading...". And when I try to delete all records from the datatable (so the table from the database get cleared), it doesn clean from the datatable.

    This is what happen:

    • When I delete 1 record and there are more records: No problem, records deleted and table ajax reloaded.
    • When I delete multiple records but not all: No problem, records deleted and table ajax reloaded.
    • When I delete 1 record and it is the last one of the table: Ajax reloading doesnt work, but if reload the page, it is cleared.
    • When I delete all records: Ajax reloading doesnt work, but if reload the page, it is cleared.

    I need to achieve:
    * Table ajax reloaded successfully when I delete all records from the table.
    * If I load the table at the beginning or after deleting: I need to show the message "No data available"

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Could you use the debugger immediately after you've done the delete and the reload and I might be able to see what is going wrong that way.

    Allan

  • Imagchine27Imagchine27 Posts: 7Questions: 2Answers: 1
    edited November 2016

    Yes, actually it throws an error when I delete all rows and clean the whole table at the same time:

    TypeError: c is undefined;

    Description:

    Ub/<()
    datatables.min.js:159
    ra/i()
    datatables.min.js:87
    ra/n.success()
    datatables.min.js:87
    n.Callbacks/i()
    jquery.min.js:2
    n.Callbacks/j.fireWith()
    jquery.min.js:2
    y()
    jquery.min.js:4
    .send/c()

    And when I delete the last existing row of the table:

    TypeError: f is undefined;

    TypeError: c is undefined;

  • Imagchine27Imagchine27 Posts: 7Questions: 2Answers: 1
    Answer ✓

    I have just solved the problem.

    I used this code in the ajax parameters:

    "dataSrc": function(data){

    if(data == "no data"){
    return [];
    }
    else {
    return data.data;
    }
    }

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Yes, if you are returning just a plain string rather than { "data": [] } from above, then that is the perfect way of handling it.

    Allan

This discussion has been closed.