0

Basically, I put the outputted JSON in to a variable then parse that to an object, but the parser isn't working. (sorry for bad indents, I'm new to stack overflow)

var url = 'https://newsapi.org/v2/top-headlines?' +
      'country=us&' +
      'apiKey=lol_dont_take_my_api_key';
var jsonString = "";
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
 //document.getElementById("placeholder").innerHTML = this.responseText;// + "}]}";
    jsonString = this.response;
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
loadDoc();
console.log(jsonString);
var obj = JSON.parse(jsonString);
if (obj.status == "ok") {
var articles = obj.articles;
} else {
console.log("error");
}
var i;
for (i = 0; i < articles.length; i++) {
    document.getElementById("main").innerHTML += articles[i].title;
}

0 Answers0