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

JavaScript

how to write this in $.ajax()

// making an jquery shorthand for ajax request first we need to take all the data for employees in or out

//using jquery.getJSON 
var path = "../data/employees.json "; 

$.getJSON(path,function(response){
  var ul = '<ul class="bulleted">';
  $.each(response,function(index,employee){
     if(employee.inoffice===false){
        ul+='<li class="out">'+employee.name+'</li>';
     }else{
        ul+='<li class="in">'+employee.name+'</li>';
     }
  });
  // closing ul tag
  ul+='</ul>';
  $("#employeeList").html(ul);
}).fail(function(jqXHR){
  alert(jqXHR.statusText);
});

1 Answer

Steven Parker
Steven Parker
243,318 Points

According to the JQuery API Documentation, this:

$.getJSON( url [, data ] [, success ] )

is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});