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 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.