JavaScript AJAX Basics (retiring) AJAX Concepts Finish the AJAX Request

Making an AJAX request, using open method

I can't figure out what I am doing wrong. I keep getting this error: Ajax error : You need to specify the request object and use the open() method on it.

app.js
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
  if (request.readyState === 4) {
    document.getElementById("footer").innerHTML = request.responseText;
   request.open ("GET","footer.html");
  }

};
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AJAX with JavaScript</title>
  <script src="app.js"></script>
</head>
<body>
  <div id="main">
    <h1>AJAX!</h1>
  </div>
  <div id="footer"></div>
</body>
</html>

1 Answer

Aakash Srivastav
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,610 Points

Hey Taylor Roberts , you must not include request.open ("GET","footer.html"); within the callback function .
It must come after the callback function . Here is the actual code :

var request = new XMLHttpRequest();
request.onreadystatechange = function () {
  if (request.readyState === 4) {
    document.getElementById("footer").innerHTML = request.responseText;
  }
};
 request.open ("GET","footer.html");
 request.send();

Hope it helps :)