I am getting a multidimensional array in form of JSON from a php file as AJAX response , there are two values I am getting from it , name and email , I need to update name and email of two respective divs each 5 seconds.
PHP response in JSON form: {"sophia":"[email protected]"}
Below is my javascript code:
window.setInterval(function () {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
for (var index in links) {
//update name div
document.getElementById("name").innerHTML= links[index];
//update email div
document.getElementById("email").innerHTML= links[index];
}
}
}
xmlhttp.open("GET", "get_data.php", true);
xmlhttp.send();
}, 5000);
My html code:
<div id="name"></div>
<div id="email"></div>
document.getElementById("name").innerHTML= index;?JSON===JavaScriptObjectNotation. What you have is a JS object, not an array.links.sophiawill get you the email, if you don't know what the properties will be,for (var p in links) { if (links.hasOwnProperty(p)) links[p];is how you should iterate over a JS object