3

It's embarrassing to have to ask, but I freely admit I'm an unseasoned Javascript developer, and I just can't figure this out. Hopefully, this will be dead simple for someone else, and I want to thank everyone here ahead of time for the help this site continually provides me.

A couple days ago, I asked this question, and am no longer getting that error. But I've run into a wall trying to actually access the data stored in the variable. My JSON looks like this:

[
    {"id":"1","name":"Bob","haircolor":"Brown"},
    {"id":"2","name":"Carol","haircolor":"Red"}
]

It's going into a variable like this:

var people=[];
$.getJSON("php/getpeople.php", function(data){ //getpeople.php generates the JSON
    people.push(data);
});

Thanks to initializing people as an array, I no longer get any error messages. But I can't access the contents of people, either. people.length returns a 0, and people[0] and people[1] are undefined.

It's there, I know it's all there, but I'm having a devil of a time figuring out where.

8
  • Is your script returning application/json as the response MIME type? Commented Jan 29, 2013 at 17:43
  • Have you verified that data is correct? Commented Jan 29, 2013 at 17:43
  • Using Firebug or a similar tool, can you verify that the browser is actually receiving the desired JSON? Commented Jan 29, 2013 at 17:44
  • 8
    You wouldn't happen to be using people right after the ajax call would you? Commented Jan 29, 2013 at 17:44
  • 1
    Probably a duplicate: stackoverflow.com/questions/3302702/… , also relevant stackoverflow.com/questions/2942544/… Commented Jan 29, 2013 at 17:48

3 Answers 3

2

people only gets values after the ajax event happens.

Call some callback function after you put the data into the people array.

Sign up to request clarification or add additional context in comments.

Comments

1

Try with this: http://jsfiddle.net/hUq7k/

$.getJSON("php/getpeople.php", function(data){ //getpeople.php generates the JSON
    $.each(data, function(i, people){
       console.log(people.id); //<------this should output "1, 2"
    });
});

make sure you are getting the response data.

2 Comments

This was it! And it's so simple, too. I'm embarrassed at having to ask it in the first place. Thank you so much!
It worked very well, and gave me some ideas on easier debugging, too. I only recently learned about console.log(), and it has a lot of really wonderful uses.
0

The following should work, if the server is actually returning the expected JSON:

$.getJSON("php/getpeople.php", function(data){ //getpeople.php generates the JSON
    var people = data;
    alert("people.length: " + people.length);
    if (people.length > 0) {
        alert("people[0].id: " + people[0].id);
    }
});

The following should not work, i.e., people will be undefined, because $.getJSON is an asynchronous method and this code is attempting to access people before the AJAX operation has completed.

var people;
$.getJSON("php/getpeople.php", function(data){ //getpeople.php generates the JSON
    people = data;
});

alert("people.length: " + people.length);
if (people.length > 0) {
    alert("people[0].id: " + people[0].id);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.