2

I'm having trouble handling a JSON object that I'm getting back from an AJAX request.

It's a simple flat JSON object, and all I want to do is write the responses back onto the page.

Can anyone point out how to loop over these objects and output the values? I just keep getting [undefined] or [object] written

Code here:

$.ajax({  
          type: "POST",  
          url: "func/chatResponse.php",  
          data: dataString,  
          success: function() {  
            $.getJSON('func/chatResponse.php?a=jsonLatest', function(data) {
                $.each(data, function(index) {
                    //items.push('<li id="' + key + '">' + val + '</li>');
                    $('body').append('<li id="' + data.user + '">' + data.user + '</li>');
                    alert(data);
                 });
            });
            alert("done");
          }  
        });  

JSON sample here

[
    {"user":"someguy","message":"my message","timestamp":"2011-04-19 17:26:09"},
    {"user":"Cheyne","message":"Hey There ...  Nice site","timestamp":"2011-04-19 17:26:09"}
]
2
  • 1
    It looks like data is an array. Try data[index].user Commented Apr 20, 2011 at 1:23
  • I love stackoverflow, I got multiple answers within minutes. thanks guys Commented Apr 20, 2011 at 1:49

2 Answers 2

12

data is the array, while you want the items inside the array.

$.each doesn't change data to become the items, instead it passes individual items as the second parameter to the function you supply:

$.each(data, function (index, item) {
    // Use item in here
    $('body').append('<li id="' + item.user + '">' + item.user + '</li>');
});

Alternatively, you can use data[index]:

$.each(data, function (index) {
    // use data[index] in here
    $('body').append('<li id="' + data[index].user + '">' + data[index].user + '</li>');
});

By the way, avoid ugly string concatenation with:

$('<li>', {id: item.user, text: item.user}).appendTo('body');
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, thanks for this. Although im getting the error: Uncaught SyntaxError: Unexpected identifier on the body.append line. Any ideas ?
@Cheyne, can you paste the exact line that you have?
$.each(data, function(index) alert(data[index].user); });
Wait , i was missing a brace... Fixed it now.. thanks for your help!
0

What you want is

                 $.each(data, function(index) {
                    //items.push('<li id="' + key + '">' + val + '</li>');
                    $('body').append('<li id="' + data[index].user + '">' + data[index].user + '</li>');
                    alert(data);
                 });

2 Comments

$.each passes in the item in the array (data[index]) as a second param to your function, so you don't need to access it like that.
I don't know much jQuery, I was just stating the obvious here :) But it looks like there are much better solutions, so...

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.