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 trialMaria Campbell
8,641 PointsMy alert for statusText worked, but it didn't return any text. Why is that?
My alert for statusText worked, but it didn't return any text. Why is that?
Maria Campbell
8,641 Points<title>AJAX with JavaScript</title>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
if(xhr.status === 200) {
document.getElementById("ajax").innerHTML = xhr.responseText;
} else {
alert(xhr.statusText);
}
}
};
xhr.open('GET', 'missing.html');
function sendAJAX() {
xhr.send();
document.getElementById('load').style.display = 'none';
}
</script>
//What I am referring to is alert(xhr.statusText); In the video, text "File Not Found" was returned. This returned nothing. There was no indication that Dave set anything up to result in "File Not Found" either. I did get an alert, just no text. The style = 'none'; refers to the button btw.
3 Answers
jcorum
71,830 Pointschaz has a good point. You either get an alert with xhr.statusText or you get the innerHTML with the xhr.responseText. I missed your fine print, viz., that you got an alert! The Editor clearly didn't care which, as long as the code was correct. So, bottom line, if you want a response rather than an alert you'll need to make xhr.status === 200 true.
jcorum
71,830 PointsI'm a bit puzzled. I looked at your code. It seemed fine. I checked the video, thinking I could test your work there. But there are no Workspaces. So then I went to the challenge that asked for the callback function you wrote, pasted in your work, and the Editor said Voila! So there doesn't seem to be anything wrong. I'm guessing you don't have any elements on the html page you're working with with an id of ajax so there's no innerHTML to write the response to.
chaz
25,372 PointsI don't think that is the issue because the if
statement containing .innerHTML
is supposed to not be met, leading to else
running which contains the alert.
chaz
25,372 PointsI'm not entirely sure what caused your issue, but if I had to guess it might be due nesting xhr.send();
within the sendAJAX()
function since it isn't related to the button.
Again, its just a guess. Just learning all this too.
jcorum
71,830 Pointsjcorum
71,830 PointsPlease include your code and provide a link to the challenge you are working on. Otherwise how are we to know what's not working?