Processing Ajax JSON Return Array

Processing Ajax JSON Return Array

I have over 50 scripts where I use an Ajax call to populate a DataTable. All work great. Now I have the need to use an Ajax call that will access a mySQL database and return information to simply display on a page via Javascript.

Here is my Ajax code...
  1. <?php
  2. $fam_ID = 1;

  3. $output = array( "sEcho" => 1, 
  4.  "iTotalRecords" => 100, 
  5.  "iTotalDisplayRecords" => 100, 
  6.  "aaData" => array()
  7.    );
  8.    
  9. try {
  10. if ( $fam_ID != 0 ) {
  11. $name_short = "SHORT NAME";;
  12. $name_long  = "LONG NAME";
  13. $submitter  = "SUBMITTER";
  14.              $photo = "PHOTO";
  15. $caption = "CAPTION";;
  16. $text = "TEXT";

  17. $output['aaData'][] = array('name_short' => $name_short,
  18.   'name_long'  => $name_long,
  19.   'submitter'  => $submitter,
  20.   'photo'    => $photo,
  21.   'caption'    => $caption,
  22.   'text'    => $text);
  23. }
  24. }
  25. // We have a problem so report it
  26. catch(PDOException $e) {
  27. $output['aaData'][] = $e->getMessage();
  28. }

  29. echo json_encode($output); // Return results as json encoded array
This code returns the following...
  1. {"sEcho":1,"iTotalRecords":100,"iTotalDisplayRecords":100,"aaData":[{"name_short":"SHORT NAME","name_long":"LONG NAME","submitter":"SUBMITTER","photo":"PHOTO","caption":"CAPTION","text":"TEXT"}]}
I am using the following code to access the Ajax and process the return array.
  1. var request;
  2. // **************************************************************************************************** Initialize jQuery
  3. $(document).ready(function() {
  4. 'use strict';
  5. //var jsonData = '{"name_short":"SHORT NAME","name_long":"LONG NAME","submitter":"SUBMITTER","photo":"PHOTO","caption":"CAPTION","text":"TEXT"}';
  6. $('#table_FamilyPages').on('click', 'tbody tr', function() {
  7. console.info( 'fam_ID: ' + oTable.row(this).cell(this, 1).data());
  8. var famID = oTable.row(this).cell(this, 1).data();
  9. request = $.ajax({
  10. url: "files_ajax/ajax_FamilyPage_Single.php?fam_ID=" + famID,
  11. dataType: "json"
  12. });
  13. // callback handler that will be called on success
  14. request.done(function () {
  15. $('#pages_remote').hide();
  16. $('#pages_local').hide();
  17. // Code here to display page info
  18. var aData = jQuery.parseJSON(request.responseJSON);
  19. document.getElementById('idLongName').innerHTML = aData.name_long; //"Long Name";
  20. $('#pages_family').show();
  21. });
  22. // callback handler that will be called on failure
  23. request.fail(function (jqXHR, textStatus, errorThrown){
  24. // Inform user of the error and log to console for debugging
  25. var errMessage = "The following error occured: " + textStatus + ' - # ' + errorThrown;
  26. alert(errMessage);
  27. console.error(errMessage);
  28. });
  29. // callback handler that will be called regardless if failed or succeeded
  30. request.always(function () {
  31. // reenable the inputs
  32. });
  33. });
  34. });
The above produces the following error at line 24
  1. SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
Removing the comment tag at line 6 and changing line 24 to 
  1. var aData = jQuery.parseJSON(jsonData);
results in a valid result.

I assume that the problem is with the Ajax code or with the use of the returned array. Given that I am a novice in this area I have no clue as to what to do. All of the on-line examples I can find make use of the in-line array which really doesn't address the issue of using an Ajax call.

TIA for your assistance.
jdadwilson