PHP动态JSON无效

时间:2018-08-14 21:06:43

标签: php json dynamic datatable

我正在尝试在名为“ locations.php”的页面上获取数据表,但仍然收到JSON无效错误。 我使用“ get_SR_Locations.php”上的以下php代码动态生成JSON:

$myjson = array();
foreach ($locationList as $list) :
  $json= array(
    "id" => addslashes($list['id']),
    "name" => addslashes($list['name']),
  "address" => addslashes($list['address']),
    "telephone" => addslashes($list['telephone']),
  "emailaddress" => addslashes($list['email'])
    );
    array_push($myjson, $json);
endforeach; 
$mj=array("data"=>$myjson);
echo json_encode($mj);

生成的JSON看起来像这样:

  

{“数据”:[{“ id”:“ 108”,“名称”:“ Sportpark”,“地址”:“ Karspstreet 501”,“电话”:“ 0123456789”,“电子邮件地址”:“ sport @ mail.com“},{” id“:” 2“,”名称“:” Blaashal“,”地址“:” Gustavstreet 2920“,”电话“:”“,”电子邮件地址“:” sporting@mailing.com“ }]}

因此,当我将其放入JSON验证器时,它会显示:有效JSON 但是我仍然收到ajax的无效JSON警告。

在locations.php上,我有以下代码: HTML

    <table id="displayTable" class="table table-striped table-bordered display compact" style="width:100%">
   <thead>
   <tr>
    <th>&nbsp;</th>
       <th>Name</th>
       <th>Address</th>
       <th>Telephone</th>
</tr>
   </thead>
   <tfoot>
   <tr>
    <th>&nbsp;</th>
       <th>Name</th>
       <th>Address</th>
       <th>Telephone</th>
   </tr>
   </tfoot>
  </table>

还有这个JQUERY

function format ( d ) {
    // `d` is the original data object for the row
    return 'So Far .... NOT so Good';
}

 $('#SR_sendForm').click(function(){
                    $('#displayTable').DataTable(
                        {
                        "order": [[ 1, "asc" ]],
                        "scrollY": $(window).height()-($(window).height()/10),
                            "scrollX": true,
                            "ajax": 'pages/get_SR_Locations.php?fdr='+$('#FDR').val()+'&s_term='+$('#searchterm').val()+'&city='+$('#city').val()+'&searchgroup='+$('#searchgroup').val(),
                            "columns": [
                            {
                                "className":      'details-control',
                                "orderable":      false,
                                "data":           null,
                                "defaultContent": ''
                            },
                            { "data": "name" },
                            { "data": "address" },
                            { "data": "telephone" }
                        ]
                        }
                    );
                $('#responseTable').show();
        }
    }); 

    // Add event listener for opening and closing details
    $('#responseTable tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row( tr );

        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
        }
    } );

这是我得到的错误:

  

DataTables警告:表id = displayTable-无效的JSON响应。有关此错误的更多信息,请参见http://datatables.net/tn/1

怎么了?

1 个答案:

答案 0 :(得分:0)

谢谢杰夫。...我按照你的意思做了...向数据中添加json而不是通过ajax。 因此解决方案是:

我做了一个tempDIV来从get_SR_Locations.php加载数据

<div class="row" id="tempDIV" style="display: none"></div>

然后我加载数据,并使用回调进行错误处理等。

$('#tempDIV').load('pages/get_SR_Locations.php?fdr='+$('#FDR').val()+'&s_term='+$('#searchterm').val()+'&city='+$('#city').val()+'&searchgroup='+$('#searchgroup').val(), function(responseTxt, statusTxt, xhr) {
            if(statusTxt == "success"){
                $('#displayTable').DataTable(
                    {

                    "order": [[ 1, "asc" ]],
                    "scrollY": $(window).height()-($(window).height()/10),
                        "scrollX": true,
                        "data": JSON.parse($.trim($('#tempDIV').text())),
                        "columns": [
                        {
                            "className":      'details-control',
                            "orderable":      false,
                            "data":           null,
                            "defaultContent": ''
                        },
                        { "data": "name" },
                        { "data": "address" },
                        { "data": "telephone" }
                    ]
                    }
                );
            $('#responseTable').show();
       }
            if(statusTxt == "error"){
            $('#responseTable').html("<b>Oops</b><br>Houston we have a problem");
            alert("Error: " + xhr.status + ": " + xhr.statusText);
            $('#responseTable').show();
        }   
        });

以及get_SR_Locations.php中的最后修改:

我删除了这一行:

$ mj = array(“ data” => $ myjson);

 $myjson = array();
foreach ($locationList as $list) :
  $json= array(
    "id" => addslashes($list['id']),
    "name" => addslashes($list['name']),
  "address" => addslashes($list['address']),
    "telephone" => addslashes($list['telephone']),
  "emailaddress" => addslashes($list['email'])
    );
    array_push($myjson, $json);
endforeach; 
echo json_encode($myjson);