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) Programming AJAX AJAX Callbacks

Ben Stevenson
Ben Stevenson
9,360 Points

AJAX requests don't seem to work via workspaces. I'm following the code as per the "AJAX Callbacks" video. Any ideas?

Here is a code excerpt from the HTML in workspaces:

The idea of the example is to use AJAX to request a missing file in order to make the alert box pop up.

When I preview this in workspaces as Dave Mcfarland does, nothing happens for me.

Am i doing something wrong?

<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'); xhr.send(); </script>

1 Answer

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Ben Stevenson

You're not doing anything wrong. We've changed how Workspaces works -- if you request a file that doesn't exist, Workspace loads the index.html file for that workspace instead of returning a 404, File Not Found Error.

The code isn't working for you, because you'll never get a 404 error in a workspace (when I shot the screencast, workspaces DID return 404 errors).

Note that MOST web servers aren't configured this way -- on most servers you WILL receive a 404 error if you request file that doesn't exist. If you have access to either a web server on your computer or one at a web hosting company, you could try the code there. It should work.

Sorry for the confusion. I'll add this information to the Teacher's notes for this video.