JSON format write to HTML

Hello, i need to retrieve JSON DATA from url and write it to HTML table, but…in console is everything okay, but when i try to write it to HTML, it wrote only [object][object] etc…

My HTML code where i need to put data(table) is div “tabulka”:

        <div id="data-show"></div>
        <div id="data-celkem"></div>
        <div id="tabulka" border="1">
        </div>
    </div>
  </div>
  </body>
</html>  

and JAVASCRIPT CODE:

var data_array = [];
var root = 'http://192.168.2.238:57771/rest/control/getReleases';


$(document).ready(function () {
    
    jQuery.support.cors = true;
    
    $('#data-load').click(function () {
        var showData = $('#data-show');
        $("#loader").show();
        $.ajax(
        {
        type: "POST",
        url: root,
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function (data) { 
            console.log(data);
                /*
                $.each(data, function (i, item) {
                    $("#tabulka").html('<div>' + data + '</div>');
                }); 
                */                                              
                   
                data.forEach(myFunction);

                function myFunction (item, index) {

                  for( var key in item ) {
                      $('#tabulka').append('data-key', JSON.stringify(data));
                  }
                }
            
            $( function() {
                $( "#dialog" ).slideDown(500);
                $( "#dialog" ).fadeOut(5000);
                $("#loader").hide();
            });
        }  
    });
  });   
});

because that is the default string representation of an object.

1 Like

Now i found solution. I hope it will be helpful for somebody.

data.forEach(function(item, index) {
  var $itemDiv = $("<div></div>");
  $itemDiv.append('<div class="hash">sha256: ' + item.sha256 + "</div>");
  $itemDiv.append(
    '<div class="timestamp">timestamp: ' + item.timestamp + "</div>"
  );
  $("#tabulka").append($itemDiv);
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.