Continuing to develop my first web-service, faced a new problem... I have a javascript function, which is provide a JSON object as array.
function RequestData(i){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/getitemID='+i, true);
xhr.send();
xhr.onreadystatechange = function() { // (3)
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
alert(xhr.status + ': ' + xhr.statusText);
} else {
alert(xhr.responseText);
var jsonData = JSON.parse(xhr.responseText);
for (var j=0; j< jsonData.length; j++ ){
alert(jsonData[j].name);
}
}
}
xhr.close();
}
there is an array with entities in jsonData like "name", "description", etc
The question is, how can i display a resulting JSON array on the html body of the page? like in for
or foreach
cycle
need just a simple example. imagine this how the JS file make the content of page
var contentString = '<div id="content">'+
'<div id="bodyContent">'+
'<button onclick="RequestData('+i+')">Load Data!</button>'+
'</div>'+
'</div>';
i want to insert a result from RequestData() function into the content of variable "var contentString"