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) AJAX Concepts A Simple AJAX Example

AJAX does not load

My AJAX div does not load. What is wrong. example1.html and example2.html doesn't work either. Here's my code: https://teamtreehouse.com/workspaces/23288522

Thanks a bunch!

Artem Shturkin
Artem Shturkin
Courses Plus Student 8,359 Points

Hello, if you want to get help, please write the code directly in the question, because the workspase/preview of workspace is closed.

6 Answers

Artem Shturkin
PLUS
Artem Shturkin
Courses Plus Student 8,359 Points

One more thing

xhr.onreadyStateChange = function() { //wrong
xhr.onreadystatechange = function() { //right, use lower case letters
Artem Shturkin
PLUS
Artem Shturkin
Courses Plus Student 8,359 Points

So, finaly I get it! I suppose that the problem was , that you wanted to preview the workspase, editing the example2.html, during the workspace preview shows you the result stored in index.html, so change the code in index.html file , and you will get a result.

I got it. Rename to index.html to preview that specific file (example1 & 2). Thank you There has to be something wrong with the code that i posted above though. I don't get any errors in console or the Ajax content to show up.

Artem Shturkin
Artem Shturkin
Courses Plus Student 8,359 Points

dont forget to choose best answer to close se discution

But the problem is not solved. There is a problem with the code that i posted above^

Artem Shturkin
Artem Shturkin
Courses Plus Student 8,359 Points

Its works, try to add console.log(xhr.responseText); to see that you get

It does not work! Copied and pasted the code given above although I had already found the error. Also this is a completely different (glad to know more ways) but much more complicated way to make ajax requests than I previously was taught.

Avery Kachline
Avery Kachline
12,824 Points

I tried to us Firefox browsers and Google Chrome both of them example to with the button does not work. Maybe we just need to you our own local sever or just use a remote one from a hosting company. For whatever reason I cannot, get example 2 to run on [Workspaces].

Oh. sorry! here it is:

  <script>
    var xhr = new XMLHttpRequest(); // step 1
    xhr.onreadyStateChange = function() { // step 2 
      if(xhr.readyState === 4) {
        document.getElementById('ajax').innerHTML = xhr.responseText;
      }
    };
    xhr.open('GET', 'sidebar.html'); // step 3
    xhr.send(); // step 4
  </script>