-1

1st time experimenting with JSON data and I was hoping to get some clarity on my issue.

when is use .getJSON on a local file I get a neat stucture like:

enter image description here

I have no problem getting the value I need (CustRep) with something like:

$.each( data.result, function( key, val ) { 
  //console.log(val.CustRep);    
  });

Now if I pull the same data source from a server I get something like this: enter image description here

I just can't figure out the correct way to go through this structure so I can access the results array I need.

Can anyone please help explain to a JS novice why I am getting different structures on what seems to be the same source and how to properly navigate the 2nd structure so I can get the values for each CustRep?

Thanks!

Ionko Gueorguiev
  • 302
  • 4
  • 18
  • 1
    Are you looking for `data.responseJSON.result` ? – Vasan Oct 27 '16 at 21:20
  • 1
    It rather looks as if in the second case, you're accessing the wrong object, namely the XMLHttpRequest object. –  Oct 27 '16 at 21:22
  • Possible duplicate of [How retrieve responseJSON property of a jquery $.ajax object](http://stackoverflow.com/questions/23681221/how-retrieve-responsejson-property-of-a-jquery-ajax-object) – Robiseb Oct 27 '16 at 21:22
  • yes, but I am looking to get jsonData.responseJSON.result but everything I tried returned "undefined" – Ionko Gueorguiev Oct 27 '16 at 21:24
  • You'll have to show the code. – JJJ Oct 27 '16 at 21:24

1 Answers1

1

You basically go about it the same way as before, just with error checks:

if (data.status === 200) { //This is good to have
   if (data.responseJSON && data.responseJSON.result) {
       var results = data.responseJSON.result;
       handleResults(results);
   }
}
Steeno
  • 137
  • 5
  • The data seems to be undefinedand and the if check fails. When I try: console.log(jsonData); - Get the info from my screen shot console.log(jsonData.status); - undefined I must be missing something really basic here o.O – Ionko Gueorguiev Oct 27 '16 at 21:37