0

Hello,
I have written an ajax call:

$.ajax({
                url:'summary_page_data',
                type:'POST',
                data:name={},
                context: this,
                dataType: 'json',
                success:function(data_){
                                        if(data_!= -1)
                                        {
                                            data=JSON.stringify(data_);
                                            alert(data);
                                            $('#write_table').append('<p>Summary:<br><br>[FORM TABLE HERE]</p>');
                                        }
                                        else
                                            alert("outside")
                                    }
                        });

In this "summary_page_data" i have written echo json_encode($skills);
SO it is returning array.
when i alert the return data,
it shows me [{"field_name":"Operating System","field_value":"Linux"},{"field_name":"Operating System","field_value":"Protocols"}]

Now i want to make an array of this return .
and print that array in table

But how to form an array of json return?

3
Contributors
4
Replies
14
Views
3 Years
Discussion Span
Last Post by Airshow
0

What you show in the question would appear to be the returned JSON, decoded then re-encoded (re-stringified).

It woud be more useful to see the raw JSON, which may or may not be the same as the re-encoded JSON. To do this, temporarily use dataType:'text', and alert (data_).

Edited by Airshow

0

Hi,
Thanks for your inputs
i did

$.getJSON('summary_page_data', function(data) {
         $('#write_table').append('<p>');
        $('#write_table').append('<label for="Summary" style="width:170px!important;">Summary</label><br>');
        $.each(data, function(index) {
                        $('#write_table').append('<tr><td>'+data[index].field_name+':'+data[index].field_value+'</td></tr><br>');
        });
        $('#write_table').append('</p>');
    });

like

0

Be careful ...

  1. jq.append() doesn't work the way you think. .append('<p>') will append an entire p node and is equivalent to .append('<p></p>') or (more economically) .append('<p/>'). Although it is syntactically incorrect, .append('</p>') will append another etire p node.
  2. With regard to node hierarchy, populating a DOM from jQuery obeys the same rules as populating from HTML. A table element will only accept thead, tbody and tr nodes. Browsers are error-tolerant but the rendering of any other node type appended to a table is indeterminate - typically above the table.

I expect you want something like this :

$.getJSON('summary_page_data').done(function(data) {
    var $tbody = $('<tbody/>').appendTo('#write_table');
    $tbody.append('<tr><td><label for="Summary" style="width:170px!important;">Summary</label></td></tr>');
    $.each(data, function(index, item) {
        $tbody.append('<tr><td>' + item.field_name + ':' + item.field_value + '</td></tr>');
    });
});

Edited by Airshow

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.