Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AJAX Basics (retiring) jQuery and AJAX Stage 3 Challenge

My solution using $.ajax()

$.ajax('data/rooms.json',{
  type: 'json',
  method: 'GET',
  success: (rooms) => {
    let html = '<ul class="rooms">';
    let $div = $('#roomList'); 
    $.each(rooms,(index,room) => {
      let roomNum = room.room;
      let isAvailable = room.available ? 'empty' : 'full';
      html += `<li class="${isAvailable}">${roomNum}</li>`;
    });
    html += '</ul>';
    $div.html(html);
  },
  error: (jqXHR) => {
    let $div = $('#roomList'); 
    $div.html(`<p>Error: ${jqXHR.status} - File: ${jqXHR.statusText}!</p>`);
  }
});
Petar Karagenov
Petar Karagenov
8,608 Points

Why are you using //type : 'json'// ? In the documentary for the $.ajax method it said that //type// is "An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0." So I think it really should be //type: 'GET'//.

Ruben Dario Avila Home
Ruben Dario Avila Home
8,806 Points

Hey but I can't use your form:

<li class="${isAvailable}">${roomNum}</li>

and I change to:

'<li class="' + isAvailable + '">' + roomNum + ' </li>';

Why you use the ${isAvailable}, ${roomNum} any documentation of this?

2 Answers

Ruben Dario Avila Home
Ruben Dario Avila Home
8,806 Points

Really Nice Solution, elegant and refined! Soo Cool! Good job :)

Michael Moore
Michael Moore
7,121 Points

I learned things about javascript from your answer, lol. Nice answer, really.