DataTables warning: table id = T_Pd - Invalid JSON response

2

I have the JSON invalid response problem but the answer is correct.

My problem is that I want to bring data to a JQuery DataTable, by means of an AJAX query sending 3 parameters. the part that fails is when it brings the answer in JSON format, it tells me that it is invalid, but it is well formatted. someone to guide me?

CONSULTATION     

        $Q= 'CALL 'sel_pd_cte'('.$id.', "'.$_POST['ini'].'", "'.$_POST['fin'].'")';

        $QE=$enlace->Query($Q);

        $i=0;

        while ($row=mysqli_fetch_row($QE)) {

            $Tpd["Data"][$i][]= array_map("utf8_encode", $row);
            $i=$i + 1;
        }

          echo json_encode($Tpd);
?>

ANSWER:

{"Data":[[["1","Razon 2","2017-05-10","8000.00","Aprobado","Entregado"]],[["2","Razon 2","2017-05-15","5000.00","Aprobado","Entrega Parcial"]]]}

Bring the result from PHP:

 Pedidos(){
      $('#T_Pd').DataTable({
                   "destroy":true,
                    "ajax":{
                        "method": "POST",
                        "url": "tped.php"
                      },

                       "columns":[
                            {"Data":"0"},
                            {"Data":"1"},
                            {"Data":"2"},
                            {"Data":"3"},
                            {"Data":"4"},
                            {"Data":"5"}
                          ]
          })
    };
    
asked by Cirel Romeo 05.06.2017 в 22:36
source

1 answer

0

Your problem is that you receive in this way

{
  "Data": [
    [
      ["1", "Razon 2", "2017-05-10", "8000.00", "Aprobado", "Entregado"]
    ],
    [
      ["2", "Razon 2", "2017-05-15", "5000.00", "Aprobado", "Entrega Parcial"]
    ]
  ]
}

when you should receive this:

{
  "Data": [
    ["1", "Razon 2", "2017-05-10", "8000.00", "Aprobado", "Entregado"],
    ["2", "Razon 2", "2017-05-15", "5000.00", "Aprobado", "Entrega Parcial"]
  ]
}

The point is that you send an array of matrices

from what I see in your php you have this:

 while ($row=mysqli_fetch_row($QE)) {
   $Tpd["Data"][$i][]= array_map("utf8_encode", $row);
   $i=$i + 1;
 }

Leave it this way:

 while ($row=mysqli_fetch_row($QE)) {
   array_push($Tpd["Data"],array_map("utf8_encode", $row));
 }
    
answered by 05.06.2017 в 23:12