Hi all,
I’m a bit confused about this challenge:
The challenge itself is easy, and I am able to put the right code:
req = new XMLHttpRequest();
req.open("GET", "/json/cats.json",true);
req.send();
req.onload = function() {
json = JSON.parse(req.responseText);
document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json);
};
However, I am not sure if I understand the point of parsing the responseText
as a JS object, only to parse it as a string again.
That is, is there a reason why we are not just putting the responseText
directly into the innerHTML?
req = new XMLHttpRequest();
req.open("GET", "/json/cats.json",true);
req.send();
req.onload = function() {
document.getElementsByClassName('message')[0].innerHTML = req.responseText
};
Note: In fact, it seems setting innerHTML = req.responseText is an accepted solution. I’m still wondering if there is a reason to follow the sample code in the lesson.