Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Maldonado
18,803 Pointswhere is the argument "response" coming from?
all of this makes sense to me, except I'm still having a difficult time understanding where the "response" argument is coming from and how it's implemented.
could someone be so kind as to explain?
$(document).ready(function(){
var url = "../data/employees.json"
//$.getJSON(url, data, callback);
$.getJSON(url, function(response){
var statusHTML = '<ul class="bulleted">';
$.each(response, function(index, employee){
if(employee.inoffice === true) {
statusHTML += '<li class="in">';
} else {
statusHTML += '<li class="out">';
}
statusHTML += employee.name + '</li>';
});
statusHTML += '</ul>';
$('#employeeList').html(statusHTML);
}); //end getJSON
}); //end ready
3 Answers
Tom Sager
18,987 PointsFor getJSON, the data and callback arguments are optional. Your code does not include a data argument. So in this case a more descriptive comment in line 3 would be
// $.getJSON(url, callback);
The getJSON call does a
GET "../data/employees.json"
and expects to receive a JSON structure from the server. The JSON is then parsed into a javascript object named "response". The subsequent code iterates over this object.
Daniel Maldonado
18,803 Pointsso "response" could be named anything? and "response" is simply the json file turned into a JS object? do i understand that correctly?
Tom Sager
18,987 PointsYes you could name it anything. It is simply the name of the argument to an anonymous callback function. This should work as well:
$.getJSON(url, function( data ) {
$.each(data, function(index, employee) {
...