如何迭代JSON数组?控制台输出:
未捕获的TypeError:无法读取属性' index1'未定义的 在Object.success(index.html:29)
变量data
中的格式:
[{"index1":2325,"proto_name":"SSH","byte_count":8523026},
{"index1":2325,"proto_name":"HTTP","byte_count":2123844},
{"index1":2325,"proto_name":"ab", "byte_count":232}
]
JS代码:
<script type="text/javascript">
function LoadTraffic() {
$(function() {
var url = "traffic";
$.getJSON( url,
function(data){
for (var i = 0; i <data.length; i++) {
var result = data[i];
var $tr = $('<tr/>');
$tr.append($('<td/>').html(result.index1));
$tr.append($('<td/>').html(result.proto_name));
$tr.append($('<td/>').html(result.ts));
$('.list-order tr:last').before($tr);
}
});
});
}
setInterval( LoadTraffic, 5000 ); <!--1s-->
</script>