Results 1 to 2 of 2

Thread: Ajax Return Array

  1. #1
    Join Date
    Feb 2013
    Location
    California
    Posts
    86
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Ajax Return Array

    I have a problem with the Javascript processing of an Ajax returned array.

    This is the code for the Ajax call:
    Code:
    var request;
    var aaData;
    // Initialize jQuery
    $(document).ready(function() {
    	'use strict';
    	
    	$('#table_FamilyPages').on('click', 'tbody tr', function() {
    		console.info( 'fam_ID: ' + oTable.row(this).cell(this, 1).data());
    		var famID = oTable.row(this).cell(this, 1).data();
    		
    		request = $.ajax({
    			url: "files_ajax/ajax_FamilyPage_Single.php?fam_ID=" + famID,
    			dataType: "json"
    		});
    	
    		// callback handler that will be called on success
    		request.done(function () {
    			$('#pages_remote').hide();
    			$('#pages_local').hide();
    			
    			// Code here to display page info
    			var aaData = JSON.parse(request);
    			document.getElementById('idLongName').innerHTML = aaData.name_long;
    			
    			$('#pages_family').show();
    		});
    	
    		// callback handler that will be called on failure
    		request.fail(function (jqXHR, textStatus, errorThrown){
    			// Inform user of the error and log to console for debugging
    			var errMessage = "The following error occured: " + textStatus + ' - # ' + errorThrown;
    			alert(errMessage);
    			console.error(errMessage);
    		});
    	
    		// callback handler that will be called regardless if failed or succeeded
    		request.always(function () {
    			// reenable the inputs
    		});
    	});
    	
    });
    The question is... What variables do I use in the JSON.parse(???) [see bold] function and then what do I use in the innerHTML line [see bold]?

    Testing the code I receive an error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

    Here is a sample of the encoded JSON array returned from the Ajax call.

    Code:
    {"sEcho":1,"iTotalRecords":100,"iTotalDisplayRecords":100,"aaData":[{"name_short":"SHORT NAME","name_long"
    :"LONG NAME","submitter":"SUBMITTER","photo":"PHOTO","caption":"CAPTION","text":"TEXT"}]}
    This is a link to my test page: http://www.txfannin.org/new-site/familypages.php

    TIA for your assistance
    jdadwilson

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I don't see any bold. The error I'm getting is on this line:

    Code:
    var aaData = JSON.parse(request);
    I don't think you want to be parsing the request object, that's the entire request. It looks like you're in luck though. The console gives up a request.responseJSON object that you should be able to have better luck working with in your code, by making the above code block:

    Code:
    var aaData = JSON.parse(request.responseJSON);
    There may still be other errors, but at least you should be working with a valid JSON object. BTW, jQuery has its own JSON parsing function. In modern browsers, the native one you are using is probably fine, but if backward compatibility is of interest, you may want to use the jQuery one.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. AJAX 'undefined' return in IE, please help!
    By profug0 in forum JavaScript
    Replies: 2
    Last Post: 11-06-2007, 05:36 PM
  2. Return array from java file to JSP
    By melvinoyh in forum Other
    Replies: 4
    Last Post: 06-29-2006, 04:14 PM
  3. Return array from java file to JSP
    By melvinoyh in forum JavaScript
    Replies: 1
    Last Post: 06-29-2006, 01:06 AM
  4. Help: return window.open array
    By simplify-i-t in forum JavaScript
    Replies: 5
    Last Post: 08-20-2005, 04:56 PM
  5. Help: return window.open array
    By simplify-i-t in forum HTML
    Replies: 0
    Last Post: 08-20-2005, 06:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •