将php数组呈现给客户端时的jQuery DataTable错误

时间:2018-11-27 12:14:19

标签: javascript php jquery datatables

我已经从PHP页面发送了json_encoded数据:

$arrayForDataTablePopulation = array();

        while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) {

                $a = trim($row["IDCONF"]);
                $b = trim($row["NOME"]);
                $c = trim($row["TIPOLOGIA"]);
                $d = trim($row["STATO"]);
                $e = trim($row["AMBIENTE"]);
                $f = trim($row["AGGREGATORE"]);
                $f2 = trim($row["NOMECED"]);
                $g = trim($row["MODELLO"]);
                $h = trim($row["CONTRATTO"]);
                $i = trim($row["VALIDADAL"]);
                $l = trim($row["VALIDAAL"]);

            if ($count < $NUM){

                $arrayForDataTablePopulation[$count] = "[" .  $a . "," .  $b . "," . $c . "," . $d . "," . $e . "," . $f . "," . $f2 . "," . $g . "," . $h . "," . $i . "," . $l . " ]";
                $count++;

            } else {

                $arrayForDataTablePopulation[$count] = "[" .  $a . "," .  $b . "," . $c . "," . $d . "," . $e . "," . $f . "," . $f2 . "," . $g . "," . $h . "," . $i . "," . $l . " ]";

            }

        }

        echo  json_encode($arrayForDataTablePopulation);

} else {

    echo "No data Received. Dimensione array: " . $receivedArrayFormSize;

}

我不知道为什么我的输出不好:

enter image description here

我认为我从服务器对阵列进行了错误编码。 Ajax方面是:

$.ajax({

                url: "asset_GestAsset.php",
                type: "POST",
                data: { "fieldValue" : JSON.stringify(arrForm) },
                success: function(data) {

                    var tableData = JSON.parse(data);

                    console.log(" Converti in stringhe: " + data.toString() + " !!! Everything ok");

                    $('#OUTCOME_RESEARCH_TABLE').DataTable({
                         "aaData":tableData,
                         "deferLoading": 57,
                         "deferRender": true,
                         "scrollY": 150,
                         "iDisplayLength": 100,
                         "scrollX": true,
                         "bRetrieve": true,
                         "bDestroy": true,
                         "ordering": false,
                         "info":     true,
                         "sDom":     'ltipr',
                         "bDestroy": true

                 });

etc...

问题是我对所有这些Array / JSON的东西感到困惑,我认为我现在在脑海中非常困惑xD 有人可以帮助我找出解决此问题的方法吗? PHP方面是错误的还是AJAX中的代码?

我想做的是修改服务器阵列,发送回客户端并正确呈现DataTable。

1 个答案:

答案 0 :(得分:1)

PHP

您应使用以下代码填充数组:

$arrayForDataTablePopulation[$count] = array($a, $b, $c, $d, $e, $f, $f2, $g, $h, $i, $l);

要打印JSON,请使用以下代码:

header('Content-type: application/json');
echo json_encode($arrayForDataTablePopulation);

JS

success处理程序中使用以下代码:

$('#OUTCOME_RESEARCH_TABLE').DataTable({
     "dom": 'ltipr',
     "data": data,
     "pageLength": 100,
     "scrollY": 150,
     "scrollX": true,
     "ordering": false
});