Invalid JSON response

Invalid JSON response

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

So I'm having some difficulty getting a server side script to work. I ran the following JSON data through JSON Lint and it tells me it is valid JSON.

["7","Border Division","6","Southern Division","5","Central Division"]

My Script creates an array with an id and division name. I read the information at https://datatables.net/manual/tech-notes/1 and followed the directions.

My html looks like this...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<?php include('css_js.html'); ?>
<title>Page title</title>
</head>
<body>
<script>
$(document).ready(function() {
    $('#example').DataTable( {
        "ajax": 'ajax_test.php',
                        
                        columns: [
        { data: 'id' },
        { data: 'division' },
                        ]
    } );
} );
</script>

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>ID</th>
                <th>Division</th>
              </tr>
        </thead>
    </table>


</body>
</html>

And my ajax script looks like this...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
</head>
<body>
<?php
include('db_connect.php');
$result= $dbConn->query("SELECT lk_divisions.id, lk_divisions.division FROM lk_divisions, eow WHERE lk_divisions.id=eow.division");
$row_cnt = $result->num_rows;

$x=1;
 while($row=$result->fetch_assoc()) {
  $divisionId=$row['id'];
    $divisionName=$row['division'];
     if($x === 1) {
        $myArray=array($divisionId, $divisionName);
    }else{
                array_push($myArray, $divisionId, $divisionName);
    }
$x++;
}

$myJSON = json_encode($myArray);

echo $myJSON;

<?php
>
?>



</body>
</html>

What am I doing wrong?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    Answer ✓

    ["7","Border Division","6","Southern Division","5","Central Division"]

    Is this the full JSON response seen in the browser's developer tools?

    It may be valid per jsonlint.com but it's not a structure that Datatables support. Datatables expects the ajax response to be an array of objects or an array of arrays. This is discussed here:
    https://datatables.net/manual/data/#Data-source-types

    By default Datatables expects the row data to be in a data object. You can tell Datatables to look elsewhere in the response using the ajax.dataSrc option. This is discussed here:
    https://datatables.net/manual/ajax

    This isn't causing the invalid JSON response error but you have defined columns.data but are receiving the data in an array. This is a mismatch which will likely result in the Requested unknown parameter once you get past the invalid JSON error. When using arrays you will want something more like this example:
    https://datatables.net/examples/ajax/simple.html

    Kevin

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Yes that is the response I get in the developer tools. I'll do some reading in the links you provided to see if I can get this working. Thank you.

This discussion has been closed.