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
Guiomar Almunia
Courses Plus Student 7,062 PointsThe office proyect revisited. Calling the function callback in the get method when is not anonymus??
Hello. I am trying to make this proyect by declaring separately;
var url =" var url = "../data/employees json";" and var callback =function(response){}
and then call them both in the $.getJOSN( url, callback);
How can I declare this properly? Here is my code so far. Not a single touch in the HTML and the widget.js file is linked
$(document).ready(function(){
$.getJSON(url, callback());
// set the path to JSON file
var url = "../data/employees json";
// declare callback function
var callback= function(response){
var statusHTML ='<ul class="bulleted">'; // <ITERACION EACH
$.each(response, function(index,employee){ //$.each(data, function(index, value){});
if(employee.inoffice===true){
statusHTML += '<li class="in">';
}else{
statusHTML += '<li class="out">';
}
statusHTML += employee.name + '</li>';
});
statusHTML+= '</ul>';
$('#employeeList').html(statusHTML);
}
});
And by the way..can anyone help me with the format on posts?? dont now who to make my code looks like in text editors when posting questions.. Thanks!!
1 Answer
Steven Parker
243,318 PointsYou don't want to do this:
$.getJSON(url, callback()); // set the path to JSON file
That invokes the callback function instead of passing it. Instead do this (notice no parentheses):
$.getJSON(url, callback); // set the path to JSON file
Also, I think you substituted a space where there should be a period on this line:
var url = "../data/employees.json"; // <-- note: period added
Guiomar Almunia
Courses Plus Student 7,062 PointsThanks a lot!!
Steven Parker
243,318 PointsSteven Parker
243,318 PointsFormatting instructions are in the the Markdown Cheatsheet pop-up link below.
Remember to skip a blank line before the code block.