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

Guillermo Gomez
Guillermo Gomez
7,413 Points

Workspace is retrieving the index.html if the file doesn't exist

When I'm on the Workspace, whenever I type in an unexistent URL, in the main path or in a subfolder, the server retrieve the index.html instead a 404 error. The following is the code from the AJAX Basics Course:

<!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>
    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>
</head>
<body>
  <div class="grid-container centered">
    <div class="grid-100">
      <div class="contained">
        <div class="grid-100">
          <div class="heading">
            <h1>Bring on the AJAX</h1>
            <button id="load" onclick="sendAjax()">Bring it!</button>
          </div>
          <ul id="ajax">

          </ul>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

For the code highlighting to work in the forums you'll need to add a new line after the three backticks and html before you paste in the code to highlight.

e.g.

```html
<!DOCTYPE html>
<html>
<body>
  <h1>Heading 1</h1>
</body>
</html>
```

2 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Guillermo Gomez

Thanks for pointing this out. You're right, you literally can't get a 404 error in the workspace right at this moment -- it loads the index.html file if you request a file that doesn't exisit. This is NOT a common behavior -- most web servers DO NOT act this way: they return a 404 if you request a resource that doesn't exist. I believe we are going to change the workspace behavior BACK to returning a 404 error, since that more accurately reflects how it works in the real world.

Guillermo Gomez
Guillermo Gomez
7,413 Points

Thank you very much for your answer, I've been having troubles with this when I tried to make intentionally lost AJAX calls or when I had some typos.

In regards to your question (or statement?), it's probably so that the user can call their base html file anything they want and it will still act like the index page. Otherwise there would need to be some sort of routing rules put in place for each specific Workspace to match the files the user creates.

Treehouse could either:

  1. Force users to use index.html (making things confusing across different stages in the same course)
  2. Add an option in the Workspaces to mark a page as the 'home' or 'index' page (adding complexity to the structure of each workspace)
  3. Leave it as is

Personally, I don't see it as being a big issue, since most users will prefer to see something familiar than a big scary error message or a generic 404 page.