Hello,
I am working on the challenge Get JSON with the JavaScript XMLHttpRequest Method and I had a question about parsing the JSON data.
Here is the example code:
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload = function(){
const json = JSON.parse(req.responseText); *
document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json);
};
My question is, if the GET request returns the responseText as a string by default, then wouldn’t it be redundant to use JSON.parse() in line * only to JSON.stringify() it back to a string in the next line?
Couldn’t we just add the responseText to the HTML element by doing something like:
document.querySelector('.message').innerHTML = req.responseText
Thank you.
