Fill JSON data into DataTable using Ajax

Fill JSON data into DataTable using Ajax

orik.wsorik.ws Posts: 5Questions: 2Answers: 0

Hello,

I need to feel JSON data to my datatable, but it shows me Invalid JSON response error on datatable load. It works fine if copy JSON data to txt file and change .php file to .txt file. Can not catch where am i wrong. Codes works fine for other people, but nor for me.
MY JSON DATA

{"data":[[3,"test","test","test\u018fL\u0130 test","test","test\u2116 test"],[4,"test","\u0130test\u0130YEV","test\u00d6V\u015e\u018fN test","test","test\u2116 test"],[5,"test\u00dcRZ\u018f","test\u0130test","test\u018fL\u0130","test","test\u2116 test"],[6,"test\u00dcSEYN","test\u00dctest","test\u011etest\u011eLU","test","test\u2116 test"],[7,"test","test\u0130ROV","test test","test","test\u2116 test"],[8,"test\u018fC\u018fB","test","\u0130BAD test","test","test\u2116 test"],[9,"test","test","test\u0130R test","test","test\u2116 test"],[10,"test\u018ftest","\u0130Stest","test\u0130Q O\u011etest","test","test\u2116 test"],[11,"test\u018fBUH\u0130","test","test\u0130F O\u011eLtest","test","test\u2116 test"],[12,"test\u018fR","\u0130test","test\u0130Q test","test","test\u2116 test"]]}

Here is my html side

<table width="100%" class="display" id="example">
  <thead>
    <tr>
      <th>ID</th>
      <th>second</th>
      <th>third</th>
      <th>fourth</th>
      <th>fifth</th>
      <th>six</th>
    </tr>
  </thead>
</table>
    $(document).ready(function() {
      $('#example').DataTable({
        "Processing": true,
          "ajax": {
        "url": "fetch.php",
        "dataSrc": ""
          },
          "columns": [
        { "data": "ID" },
        { "data": "second" },
        { "data": "third" },
        { "data": "fourth" },
        { "data": "fifth" },
        { "data": "six" }
          ]
        });
    });

Here is my code fetch.php file

<?php
include 'confx.php';
$conn = connection_to_db('table_name');
   
$query = "select top 10 ID, second, third, fourth, fifth, six from table_name";
$result = sqlsrv_query($conn, $query);
$data = array();

while($row=sqlsrv_fetch_array($result))
{
    $data['data'][] = array($row['ID'],$row['second'],$row['third'],$row['fourth'],$row['fifth'],$row['six']);
}
echo json_encode($data);

?>

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 8,033Questions: 0Answers: 1,349

    Hi @orik.ws ,

    See these two examples - here and here. Yours is a mismatch of the two - you're sending arrays, but have declared objects. Those two examples should sort you out,

    Cheers,

    Colin

  • orik.wsorik.ws Posts: 5Questions: 2Answers: 0

    Hi @colin ,

    Thank you for reply. I tried both, but no one is working for me. Now i tried object one and have same error Invalid JSON response.

    here is my JSON result

    {"data":[{"ID":3,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":4,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":5,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":6,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":7,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":9,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":10,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"},{"ID":12,"second":"test","third":"test","fourth":"test","fifth":"test","six":"test"}]}
    

    here is my js

      $('#example').DataTable({
        "Processing": true,
          "ajax": {
            "url": "fetch.php",
            "dataSrc": ""
          },
          "columns": [
            { "data": "ID" },
            { "data": "second" },
            { "data": "third" },
            { "data": "fourth" },
            { "data": "fifth" },
            { "data": "six" }
          ]  
        });
    });
    

    fetch.php file

    while($row=sqlsrv_fetch_object($result)){
        $row_array['ID'] = $row->ID;
        $row_array['second'] = $row->second;
        $row_array['third'] = $row->third;
        $row_array['fourth'] = $row->fourth;
        $row_array['fifth'] = $row->fifth;
        $row_array['six'] = $row->six;
    
        array_push($data['data'],$row_array); 
    }
    
    echo json_encode($data);
    
  • kthorngrenkthorngren Posts: 8,096Questions: 25Answers: 1,817
    Answer ✓

    Is your "JSON Result" copied from the Developer Tools Network Response tab as described here?
    https://datatables.net/manual/tech-notes/1

    Is there any other text in the Response tab?

    Kevin

  • orik.wsorik.ws Posts: 5Questions: 2Answers: 0

    Hello @kthorngren,

    Thank you for reply. Data comes from my database. View of JSON I copied in comment. If i add it to .txt file and in url change url to .txt file, it works fine, but not working with my .php file

  • orik.wsorik.ws Posts: 5Questions: 2Answers: 0

    I solved the problem, problem was comment after php code on server side, which i did not see it 2 days...

  • fhuofhuo Posts: 3Questions: 0Answers: 0

    Hi orik.ws
    I have exactly the same problem as you. When I validate the result from .txt and from .php with JSONLint both are valid.
    What did you found a comment in your php?
    Thanks for your help
    Freddy

  • fhuofhuo Posts: 3Questions: 0Answers: 0

    the problem is solved in the meantime. I had an invalid character in the datastream.
    Sorry - Freddy

Sign In or Register to comment.