API request yields no results

Tell us what’s happening:
Hello, forum helpers. I’m working through the APIs and AJAX challenges, and on the exercise below, I pass all the tests, but nothing happens when I click the button. I’m wondering what I might have done wrong and why I was able to pass the tests anyway.

On a side note, I also wonder if anyone might explain why there was no “let”, “var”, or “const” necessary in the declaration of the “request” variable? Even the FCC example in the lesson left that out.

Thanks for your time!

Your code so far


<script>
document.addEventListener('DOMContentLoaded',function(){
  document.getElementById('getMessage').onclick=function(){
    // Add your code below this line
request = new XMLHttpRequest();
request.open("GET",'/json/cats.json',true);
request.send();
request.onload=function(){
json=JSON.parse(request.responseText);
document.getElementsByClassName('message')[0].innerHTML=JSON.stringify(json);
};

    // Add your code above this line
  };
});
</script>
<style>
body {
  text-align: center;
  font-family: "Helvetica", sans-serif;
}
h1 {
  font-size: 2em;
  font-weight: bold;
}
.box {
  border-radius: 5px;
  background-color: #eee;
  padding: 20px 5px;
}
button {
  color: white;
  background-color: #4791d0;
  border-radius: 5px;
  border: 1px solid #4791d0;
  padding: 5px 10px 8px 10px;
}
button:hover {
  background-color: #0F5897;
  border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message">
The message will go here
</p>
<p>
<button id="getMessage">
  Get Message
</button>
</p>

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36.

Challenge: Get JSON with the JavaScript XMLHttpRequest Method

Link to the challenge:
https://www.freecodecamp.org/learn/data-visualization/json-apis-and-ajax/get-json-with-the-javascript-xmlhttprequest-method

They are necessary, and actually that’s the problem with the challenge. Also needed for this line:
json=JSON.parse(request.responseText);

Add const in and the button will work.

I believe this is a known problem that’s being addressed.

As MDN puts it,

Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed.

It’s a bad practice and you should not do it, but it is allowed by the language standards.

Thanks for the insight!