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 trialStephen Santiago
9,631 PointsAJAX callback function - Trying to add code to open request.
I am not sure if this is a syntax error, it seams like everything is in place but the code challenge will not let me proceed. Had an issue with the workspace in this section as well, not sure if they are related.
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState === 4) {
document.getElementById("footer").innerHTML = request.responseText;
request.open("GET","footer.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
Kristian Terziev
28,449 PointsHello Stephen,
request.onreadystatechange is a callback and it will be executed when you have completed the request. But the request can't be completed because you open it inside the callback. You have to open the request outside the callback. Like so:
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState === 4) {
document.getElementById("footer").innerHTML = request.responseText;
}
};
request.open("GET","footer.html");
Stephen Santiago
9,631 PointsStephen Santiago
9,631 PointsThank you Kristian, that worked perfectly.