0

I have the following code:

$.getJSON('../_javascripts/array.php')
    .success(function(response) { console.info(response); alert("success"); })
    .fail(function(jqXHR, status, error){ console.info(error); alert("error"); });

Which displays output within the console, however, I am trying to access that information (which is displayed within the console - it's an array) and can't seem able to.

console output

Object {P5yTZ947: Array[3], 11y6tdo8: Array[3], 66j8ttk2: Array[3], 27c7uqv0: Array[3], 44f6hvt7: Array[3]…}

I have tried the following:

alert(response);
alert(response[0]);
var array = response;

All of which bring back undefined.

Clearly I am doing something wrong but can't quite fathom what. Any advice, feedback and comments welcomed.

Homer_J
  • 3,277
  • 12
  • 45
  • 65

3 Answers3

0

From what the console outputs it looks like what array.php is returning is an object with arrays in it.

Sonicdeadlock
  • 88
  • 2
  • 9
0

Your handling of the response must be from within the callback functions given to $.getJSON();

That is, the scope of the response object in the case of success is only within the success function. Put your success handling code in there (or call it from there) and you should be good to go.

For example:

function doSomethingWithSuccessResponse( response ){
  console.log( response );

  // Execute the rest of your success case logic here
}

$.getJSON('../_javascripts/array.php')
    .success(function(response) { doSomethingWithSuccessResponse(response); })
    .fail(function(jqXHR, status, error){ console.info(error); alert("error"); });

Or, more concisely:

function doSomethingWithSuccessResponse( response ){
  console.log( response );

  // Execute the rest of your success case logic here
}

$.getJSON('../_javascripts/array.php')
    .success(doSomethingWithSuccessResponse)
    .fail(function(jqXHR, status, error){ console.info(error); alert("error"); });

Similarly for the failure case - execute your failure handling logic within the failure callback.

It looks like your API is returning an object with embedded arrays:

Object {
  P5yTZ947: Array[3], 
  11y6tdo8: Array[3], 
  66j8ttk2: Array[3], 
  27c7uqv0: Array[3], 
  44f6hvt7: Array[3]…
}

Use object-looping semantics to iterate through the object. Then use an inner loop to loop over the elements of the array:

for (var key in response) {
   if (response.hasOwnProperty(key)) {
      // key is now "P5yTZ947" or "11y6tdo8", etc
      var innerArray = response[key];

      // Loop over the values of the inner array
      for( var i = 0; i < innerArray.length; i++ ){
        console.log( innerArray[i] );  // 
      }
   }
}
Community
  • 1
  • 1
Brian
  • 1,097
  • 11
  • 11
  • Would that enable me to iterate through the array? – Homer_J Jul 08 '14 at 18:38
  • It should, yes. It looks like your API is returning an object (or dictionary/hash if thats more familiar to you). Iterating through the properties of a hash is a bit different in Javascript than iterating through an array. – Brian Jul 09 '14 at 12:33
  • For an object, iterate like this: `for (var key in response) { if (response.hasOwnProperty(key)) { console.log(key, response[key]); } }` http://stackoverflow.com/a/14379304/13264 – Brian Jul 09 '14 at 12:33
  • @Homer_J added looping logic to the answer with a bit more explanation. – Brian Jul 09 '14 at 12:43
0

It's difficult to answer you as we don't have any access to your array.php file. Try to debug the response output with the following code (you need to have the Developer Console opened):

$.getJSON('../_javascripts/array.php', function( data ) {
    $.each( data, function( key, value ) {
        console.log( key );
        for( var i in value ) {
            console.log( "  " + JSON.stringify(value[i]) );
        }
    });
});

Try to use breakpoints within Google Chrome console to inspect the data (Ajax response) variable contents.

SiZiOUS
  • 638
  • 1
  • 8
  • 9