Trying Ajax JSON object method. Not building table. No errors

Trying Ajax JSON object method. Not building table. No errors

dimicheledimichele Posts: 1Questions: 1Answers: 0

Link to test case:

Error messages shown: Alert = DataTables warning: table id=table_id - Invalid JSON response. For more information about this error, please see

Description of problem:

I'm new to

Trying to use Ajax to read states.json file. Tried two methods (see code). Current method (using "ajax": mydata) object consolesout the object just fine. However, I get the alert above with no data displayed.

Followed all the instructions per the alert at the link Everything was fine. Status 200 on everything.

Verified the JSON on JSON Lint. It was fine also.

My preferred method because I do not want to use all the columns in the database using the longer Ajax method using data: mydata and defining columns gave me numerous datatables.min.js errors. It's currently commented out.

Could not find this issue in the forums, examples, or manual.

FYI, if I use the server side method using PHP to get the data from the table all works as advertised. Would really like to do this local. This is for a mobile app and I would like to make the data available if the user is offline.

The code is pretty well commented and should be easy to figure out.

Thank you in advance!!!!!



<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <script src="/js/jQuery.3.5.1.js"></script>
   <link rel="stylesheet" type="text/css" href=""/>
   <link rel="stylesheet" type="text/css" href=""/>
   <script type="text/javascript" src=""></script>
   <link rel="stylesheet" type="text/css" href="css/backend.css"/>

   <table id="table_id" class="display" style="width: 100%;">
               <th>Adam Walsh</th>
               <th>Applies To</th>
               <th>Flagged</th>  //Commented out when using long method below
               <th>ID</th>  //Commented out when using long method  below
            <th>Adam Walsh</th>
            <th>Applies To</th>
            <th>Flagged</th>  //Commented out when using long method  below
            <th>ID</th>  //Commented out when using long method  below

<!-- Scripts -->
      $(document).ready(function() {
         console.log('Document ready');

         /* Imports the local JSON file > mydata object */
         fetchJSONFile('states.json', function(mydata){
            console.log(mydata); // This works fine.  Object is browsable

            $('#table_id').DataTable( {

               "ajax": mydata  //Commented out when using long method below

               /* Long method */
               /* Tried Ajax method below, Shows many datatables.min.js errors */
               /* Prefer this method because I don't want to use all the columns in database */

               /* data: mydata,
               columns: [
                  { data: 'stateInitials' },
                  { data: 'stateName' },
                  { data: 'distance' },
                  { data: 'awa' },
                  { data: 'appliesTo' },
                  { data: 'restrictions' },
                  { data: 'asOfDate' },
               ] */



      /* Function to fetch JSON file */
      function fetchJSONFile(path, callback) {
         var httpRequest = new XMLHttpRequest();
         httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState === 4) {
                  if (httpRequest.status === 200) {
                     var data = JSON.parse(httpRequest.responseText);
                     if (callback) callback(data);
         };'GET', path);



  • kthorngrenkthorngren Posts: 11,867Questions: 25Answers: 2,772

    In your case using "ajax": mydata will try to fetch from the URL in the variable mydata. But mydata is not a URL thus the error.

    You have /* data: mydata, which is close. You need to point to the array of rows. Try using data: mydata.posts.

    I've never used httpRequest.send(). If its an async request you will need to use a callback to initialize Datatables. Otherwise datatables may initialize before the response populates the mydata variable. You will need to look at the docs for httpRequest.send().


Sign In or Register to comment.