Bryan Edler
Bryan Edler

Reputation: 21

How do I append jQuery.ajax data to a table?

The data I am getting from $.ajax is in this format:

{
    "rows":[
        {"key":["aaa"], "value":240363},
        {"key":["sss"], "value":29},
        {"key":["ddd"], "value":240363},
        {"key":["fff"], "value":240363},
        {"key":["ggg"], "value":240363},
        {"key":["hhh"], "value":240363},
        {"key":["jjj"], "value":240363}
    ]
}

How can I append it to a table in html?

Upvotes: 0

Views: 40

Answers (2)

Kevin Bui
Kevin Bui

Reputation: 524

HTML code:

<table id="my_table"></table>

Jquery code:

$ajax(
    url: some_url_here
    // other params
    success: function(data){
        $.each(data.rows, function(index, item){
            $('#my_table').append('<tr><td>'+item.key[0]+'</td><td>'+item.value+'</td></tr>');
        }
    }
);

Hope this help

Upvotes: 1

Bryan Edler
Bryan Edler

Reputation: 21

the problem was that i was receiving a text rather than a json object. i used $.getJSON instead and the problem was solved. you can view your object by console.log(data) on order to access the parts you need from the data.

Upvotes: 0

Related Questions