How to solve length error when no data

How to solve length error when no data

mastersusemastersuse Posts: 61Questions: 28Answers: 0

I got this error when no data in the row.

$('#projectTable').DataTable({
    processing: false,
    dom: 'Bfltip',
    destroy: true,
    ajax: { 
        url: api_url,
        crossDomain : true,
        type : "POST",
        cache : false,
        dataType : "json",
        contentType: "application/json",
        dataSrc : "service",
        data: function(d) {
            return JSON.stringify({"uid": uid, "tokenid": token_id});
        }
    },
    columns: [
        { data : "name" },
        { data : "ncd_id" },
        { data : "user_label" },
        { data : "created_by" },
        { data : "modified_by" },
        { data : "type_name" },
        { data : "speed" },
        { data : "node_a" },
        { data : "node_b" },
        { data : "id" },
    ],    
});

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    What does your JSON response look like when there is no data?

    Datatables expects and empty array. You have dataSrc : "service", so your empty response should look something like this: {"service": []}.

    Kevin

  • mastersusemastersuse Posts: 61Questions: 28Answers: 0
    edited August 2020

    Backend return like this when there is no data
    {"result":"no-data","msg":"No services information for specified project folder found."}

    Hi how to apply from my code above

    like this?

    dataSrc : function(a){
        if (a == null) {
            "service": [],
        }
        else {
            "service",
        }
    }
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It should be the top one, "server": [].

    Colin

This discussion has been closed.