1

My code:

$.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"}

It works fine. But how i can handle it with multiple JSON data, like

{"username":"Aarivex","active":"1"}{"username":"Aarivex2","active":"1"}

?

1
  • 2
    Start returning an array of them. Then iterate them. It will work for 1, it will work for n. For example: [{"username":"Aarivex","active":"1"},{"username":"Aarivex2","active":"1"}] and [{"username":"Aarivex","active":"1"}] Commented Jan 5, 2016 at 20:40

1 Answer 1

1

If we assume your data is:

var data = [{"username":"Aarivex","active":"1"}, {"username":"Aarivex2","active":"1"}];

Your success function becomes:

success: function(data) {
   $.each(data, function(index, element) {
      $('#usersOnline').append($('').text(element['username']).append($('').text('aktiv')));
   });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.