I am still learning about working with json and would appreciate some guidance. The data file I need to access is located at this url -
I would like to display it in the following format:
<ul id="smenu">
<li></li>
</ul>
Could someone please help me understand how to request the data from the given url, iterate through it using a loop, and print it out? I attempted the provided code but it did not work as expected. Thank you for your assistance :)
var xmlhttp = new XMLHttpRequest();
var url = "http://www.thedailystar.net/json/category";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var out = "";
var i;
for (i = 0; i < arr.length; i++) {
out += '<li href="' + arr[i].name + '">' +
arr[i].display + '</li><br>';
}
document.getElementById("smenu").innerHTML = out;
}