使用getjson渲染数组时遇到一些问题。
我的json输出是:
{"item":[{"id":"24","utoken":"e43aa84cc304a1ed3722832616294516b118a5c2","st_type":"2","st_action":"cvb","st_situation":"cvbc"},{"id":"25","utoken":"e43aa84cc304a1ed3722832616294516b118a5c2","st_type":"1","st_action":"vbnv","st_situation":"vbnv"}]}
我的javascript是:
<script type="text/javascript">
$.getJSON('showastrategy.php?utoken=<? echo $_SESSION['utoken']; ?>',
function(data){
strategylist = data.items;
$.each(strategylist, function(index, strategy) {
$('#altcontent').append('<li>' + strategy.id + '' +
'<p>' + strategy.utoken + ' ' + strategy.st_title + '</p>' +
'<p>' + strategy.st_action + '</p>' +
'' + strategy.st_situation + '</li>');
});
});
</script>
答案 0 :(得分:0)
首先; data.items
不存在。
使用:
function(data) {
for(var i in data['item']) {
$('#altcontent').append('<li>' + data['item'][i].id + '' +
'<p>' + data['item'][i].utoken + ' ' + data['item'][i].st_title + '</p>' +
'<p>' + data['item'][i].st_action + '</p>' +
'' + data['item'][i].st_situation + '</li>');
});
}
}