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) 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();