<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>
<div id="div1">sqq</div>
<div id="div2">dv2</div>
<script>
function getData(){
$.ajax({
type:"GET",
url:"j.json",
dataType:"json",
success: function(jsondata){
output(jsondata);
}
});
}
function output(json){
var Data = eval('(' + json + ')');
var html = '';
//alert(Data.length);
for(var i=0;i<Data.length;i++){
html += 'name' + Data[i].name + 'age' + Data[i].age;
}
document.getElementById('div1').innerHTML = html;
document.getElementById('div2').innerHTML = Data[0].name;
}
setTimeout(getData, 3000);
</script>
</body>
</html>
Above is my code and json file content is:
[{"name":"aaa","age":18},{"name":"bbb","age":19}]
I don't understand why this not working. I want to update the content of div1 using json data but nothing happened. Is it anything wrong when I tried to read the json data? I'm not familiar with dealing Json data, so please explain in detail. Thanks a lot.