JavaScript AJAX Basics (retiring) Programming AJAX Create a callback

why does the editor not accept xhr.onreadystatechange(() => { }); ?

Does the editor not like anonymous functions?

I also tried xhr.onreadystatechange(function() { }); and it didn't like that either

app.js
var xhr = new XMLHttpRequest();
xhr.onreadystatechange(
  function() {
  }
);
xhr.open('GET', 'sidebar.html');
xhr.send();
index.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="sidebar"></div>
</body>
</html>

1 Answer

Hi Liam,

It's because you are trying to pass the anonymous function to xhr.onreadystatechange, as if it's a parameter.

You need to assign the function to xhr.onreadystatechange using =

For example:

xhr.onreadystatechange = function(){};

Hope this helps :)

Thank you very much.