Issue with loading from ajax json

Issue with loading from ajax json

rjpistonrjpiston Posts: 8Questions: 2Answers: 0

my json file is returning valid json in the proper format however, I'm getting the following error msg:

DataTables warning: table id=tblNotes - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

{
    "data": [{
        "noteID": "165",
        "projectID": "34",
        "noteContent": "test 2",
        "createdAt": "Sat 27 Feb 2021 06:53 am",
        "createdBy": "23",
        "noteType": "note"
    }, {
        "noteID": "164",
        "projectID": "34",
        "noteContent": "test",
        "createdAt": "Sat 27 Feb 2021 06:53 am",
        "createdBy": "23",
        "noteType": "note"
    }, {
        "noteID": "163",
        "projectID": "34",
        "noteContent": "asdf",
        "createdAt": "Sat 27 Feb 2021 06:28 am",
        "createdBy": "23",
        "noteType": "note"
    }]
}

My js file:

$('#tblNotes').DataTable( {
  ajax: {
    url: '../admin/modules/projects/functions/getProjectNotes.php',
    cache: false,
    contentType: false,
    data: function ( data ) {
      return $.extend( {}, data, {
        projectID: $('#hdnProjectID').val()
      } );
    }
  }
});

my json file

<?php
  require_once $_SERVER["DOCUMENT_ROOT"]."/admin/include/admin_init.php";
  require_once $_SERVER["DOCUMENT_ROOT"].'/admin/include/config.php';
  require_once $_SERVER["DOCUMENT_ROOT"].'/admin/modules/clients/functions/clients-functions.php';
  require_once 'projectfunctions.php';
  $errors         = array();
  $data           = array();

  $projectID = $_REQUEST["projectID"];
  if (empty($projectID))
    $errors['projectID'] = "You need to a project selected";

    if (!empty($errors)) {
      $data['success'] = false;
      $data['errors']  = $errors;
      $data['message'] = "Something went wrong, the project notes could not be loaded.";
      $data["data"][] = "";
    }else{

      $projectNotes = getProjectNotesByProjectID($projectID);
      // console_log($projectNotes);
      foreach($projectNotes as $key => $projectNotes){
        $noteID = $projectNotes['noteID'];
        $projectID = $projectNotes['projectID'];
        $noteContent = $projectNotes['noteContent'];
        $noteDate = date("D d M Y h:i a", strtotime($projectNotes['createdAt']));
        $createdAt = $noteDate;
        $createdBy = $projectNotes['createdBy'];
        $noteType = $projectNotes['noteType'];
        $data["data"][] = array("noteID" => $noteID,
          "projectID" => $projectID,
          "noteContent" => $noteContent,
          "createdAt" => $createdAt,
          "createdBy" => $createdBy,
          "noteType" => $noteType);
         }
         // $data['success'] = true;
    }
echo json_encode($data);

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Since your data for each row is an object, you need to tell DataTables what property of the object to use for each column - that is done with columns.data.

    See the manual for information on this and more.

    Allan

  • rjpistonrjpiston Posts: 8Questions: 2Answers: 0

    I'm receiving the same error even when adding the columns data

    $('#tblNotes').DataTable( {
      ajax: {
        url: '../admin/modules/projects/functions/getProjectNotes.php',
        cache: false,
        contentType: false,
        data: function ( data ) {
          return $.extend( {}, data, {
            projectID: $('#hdnProjectID').val()
          } );
        },
        columns: [
        { data: "createdAt"},
        { data: 'noteContent' },
        { data: 'noteType' },
        { data: 'noteID' }
      ]
      }
    });
    
    
  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,774
    Answer ✓

    You have the columns option inside the ajax option. Move it so it’s between lines 17 and 18.

    Kevin

  • rjpistonrjpiston Posts: 8Questions: 2Answers: 0

    /facepalm

    Thank you! and my apologies for wasting your time on such a stupid mistake on my part.

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,774

    No problem. It happens to all of us :smile:

    Kevin

This discussion has been closed.