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

Learning AJAX Basics Course. What Is The Difference Here?

So I'm halfway through the Ajax course and it's all cool so far.

I've just done the code challenge where in a JSON file you create an array of objects with staff members having a name and office status of "In" or "Out" and then in Javascript use Ajax to generate HTML.

I'm only just starting to learn so I don't understand the full power of using AJAX.

My question is... what is the difference between doing this and using AJAX/JSON to generate this and just using normal HTML & CSS?

In HTML & CSS you could create a list and simply change the staff members "in" our "out" whenever you needed.

Isn't it the same? If you needed to change it in AJAX you'd have to change the objects in the JSON file which is just the same.

What are the benefits here of using AJAX?

I understand the point of the example is to teach you how to write AJAX more than the benefit in this particular example but if you could help me understand the benefits here that would be great!

Here is the code if you've forgotten:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>ajax</title>
        <link rel="stylesheet" href="styles.css" media="screen" title="no title" charset="utf-8">
    </head>
    <body>
        <div id="employees">
        </div>
        <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script type="text/javascript" src="main.js"></script>
        <script type="text/javascript" src="main.JSON"></script>
    </body>
</html>
#employees .in::before {
    content: "In";
    display: block;
    width: 80px;
    padding: 10px;
    background: orange;
    border-radius: 5px;
    text-align: center;
    font-size: 1.2rem;
    color: white;
}

#employees .out::before {
    content: "Out";
    display: block;
    width: 80px;
    padding: 10px;
    background: black;
    border-radius: 5px;
    text-align: center;
    font-size: 1.2rem;
    color: red;
}
$(document).ready(function(){

      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
              var staff = JSON.parse(xhr.responseText);
              var htmlStatus = '<ul>';
              for (i=0; i<staff.length; i++)
              {
                 if (staff[i].workstatus === true) {
                     htmlStatus+= '<li class="in">';
                 } else {
                     htmlStatus+= '<li class="out">';

                 }
                 htmlStatus += staff[i].name;
                 htmlStatus += "</li>"
              }
          }

          htmlStatus += "</ul>";
          document.getElementById('employees').innerHTML = htmlStatus;
      }

      xhr.open("GET", "main.JSON")
      xhr.send();


});
[
    {
        "name": "Joe Consterdine",
        "workstatus": true
    },

    {
        "name": "Roberto Baggio",
        "workstatus": true
    },

    {
        "name": "Michael Smith",
        "workstatus": false
    },

    {
        "name": "Darren Huckerby",
        "workstatus": true
    },

    {
        "name": "Dean Blackwell",
        "workstatus": false
    },

    {
        "name": "Neil Sullivan",
        "workstatus": false
    },

    {
        "name": "Mark Fish",
        "workstatus": true
    },

    {
        "name": "Dean Holdsworth",
        "workstatus": true
    }

]

3 Answers

The point with the ajax is this would be a json file coming from the server. So ajax is really handy when you want to update part of a view with information from the server.

Something you see all the time is the autocomplete searches on websites. That's ajax doing that. Each keystroke sends a request to the server, the server searches it's records, and returns data that matches, where ajax parses and displays back.

The point of ajax is to be dynamic. The JSON file would be returned by the server itself, not generated by us. It's just in this example the json file is created manually. But in real life, the json file wold be created by the server and sent back to us.

Thanks Kevin,

actually now it makes sense... we'd have something like an input box to enter your name and status and then it would generate it automatically without us doing any work. Along those lines anyway!

Cheers.

If I'm understanding your question, the benefit of using AJAX vs simply manipulating the HTML is that AJAX will let you interact with the data dynamically. For example, you could have a PHP script that queries a huge database and pulls out specific records, and then generates and returns the JSON file from those records.

Without AJAX you'd have to make the user load all of the data even if you only want to use a small subset of it. It's also going to be more of a hassel to manage. If one of the employees gets married or fired then you have to manually edit your code vs pulling the freshest info from a database.

If the question is to why you update the JSON file vs just updating the HTML then you have to imagine that more than one person is referencing this schedule. If everyone is getting information from the JSON data (which would normally be the database) then you want to update the data and pull from that so that everyone is working with the same information. If you just manipulate the one page, then it's possible that Dean is out of the office on my page but in the office on someone elses.

In a real world example you would most likely not just be using AJAX to fetch a JSON file, but using it to ask a script to generate JSON data.