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) AJAX Concepts A Simple AJAX Example

Jose Balaguer
Jose Balaguer
17,473 Points

Workspace doesn't load the ajax text

The code is equal to the one in the video, letter by letter, and the preview (eye icon) only loads the first line of text (plain html).

Jose Balaguer I also cannot load the AJAX and followed Dave along in his code. I am using a Chrome browser, same as Dave's, as well.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link href='//fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/main.css">
  <title>AJAX with JavaScript</title>
  <script>
//This is the code that was inserted in the video
 var xhr = newXMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
      document.getElementById('ajax').innerHTML = xhr.responseText;
      }
    };
xhr.open('GET', 'sidebar.html');    
    function sendAJAX() {
    xhr.send();
      document.getElementById('load').style.display = "none";
    }
//end modification for a bit 
  </script>
</head>
<body>
  <div class="grid-container centered">
    <div class="grid-100">
      <div class="contained">
        <div class="grid-100">
          <div class="heading">
//This was also inserted in the video
            <h1>Bring on the AJAX</h1>
            <button id="load" onclick="sendAJAX()">Bring it!</button>
//End insertion
          </div>
          <div id="ajax">
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

1 Answer

Fernando Vidoto
Fernando Vidoto
13,323 Points

The code inside the <script></script> tag should be like:

  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'sidebar.html');
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      document.getElementById('ajax').innerHTML = xhr.responseText;
    }
  };
  xhr.send();