我的代码:
$.ajax({
url: 'online.php',
success: function(data) {
var userOnline = $.parseJSON(data);
$('#usersOnline').append($('<div id="usersOnlineUser">').text(userOnline['username']).append($('<span class="badge">').text('aktiv')));
}
});
JSON:
{"username":"Aarivex","active":"1"}
工作正常。但我如何处理多个JSON数据,如
{"username":"Aarivex","active":"1"}{"username":"Aarivex2","active":"1"}
答案 0 :(得分:1)
如果我们假设您的数据是:
var data = [{"username":"Aarivex","active":"1"}, {"username":"Aarivex2","active":"1"}];
您的成功功能变为:
success: function(data) { $.each(data, function(index, element) { $('#usersOnline').append($('').text(element['username']).append($('').text('aktiv'))); }); }