-1

My JSON string(this is what i get after making the request in $.post )-

{"email":"bill gates"}
{"email":"steve jobs"}
{"email":"mark zuckerberg"}
{"email":"cristiano ronaldo"}
{"email":"wayne rooney"}

The code I am using to get the content-

$(document).ready(function() {
    var data = new Object();
    data.email = "[email protected]";
    var dataString = $.toJSON(data);
    $.post('templates/chat.php', {
        data: dataString
    }, function(json) {
$("body").append(json);
    });
});

I want to dislay each one of them in an <li> tag.

2
  • What did you try? Rentacoder.com is just around the corner... Here at SO we answer questions about pieces of code that you have already written; this is not a "write my code for me" website. Commented Sep 7, 2011 at 10:11
  • Also, that is not valid JSON. That is five separate pieces of individually-valid JSON. Commented Sep 7, 2011 at 10:12

3 Answers 3

1

Try something like that:

var list = "<ul></ul>";
$.each(json, function(idx, value) {
    list.append("<li>" + value.email + "</li>");
});
$("body").append("list");
Sign up to request clarification or add additional context in comments.

1 Comment

I think it should be: $("body").append(list); instead of $("body").append("list");
1

take a look at http://api.jquery.com/jQuery.parseJSON/

should be able to do

 var obj = $.parseJSON(json);
 $.each(obj, function(index, item){
     // append to your <ul> if it already exists, or build one up
     $('ul').append('<li>' + item.email + '</li>');
 });

Comments

0

You don't need to manually encode things to JSON with jQuery, usually.

Does the following Javascript work instead?

$(document).ready(function() {
    var data = new Object();
    data.email = "[email protected]";
    $.post('templates/chat.php', data, function(response) {
        $("body").append('<ul>' + response.map(function(elm) {
            return '<li>' + elm + '</li>';
        }).join('') + '</ul>');
    });
});

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.