我只想在警报中打印数据,但每次它都会提醒我 [object Object] 但我希望它会显示我的 table 数据行

i just want to print the data in alert but ever time it alert me [object Object] but i want that it will show my table data row

$.post("Hirarchi/addHirarchi",
    {/*'id':hirarchiId,*/'hirarchiName':hirarchiName,'level':HirarchiLevel},
        function(data) {


            $('#lblMessage').html (data);
            $('#txtHirarchiName').val('');
            $('#txtHirarchiLevel').val('');

                        var data = $.parseJSON(data);

                        $.each(data, function(a,b,c) {


                            alert(data);

                        });

        });

不是只打印数据,而是打印如下内容:

alert(JSON.stringify(data));

看,这是否解决了您的问题。

您不能将对象传递给警报。如果你给它object,它会[Object Object]。警报参数必须是字符串

//for Each loop code
$.each(data, function(index,value,list) { 
  alert(JSON.stringify(value)); //if you want to alert each object then use
  //data will not be accessible inside $.each you can access the same using 3rd parameter which list
  alert(JSON.stringify(list));
});

但问题是为什么您在 $.each 中发出警报并警报相同的数据。 我假设它是您代码中某些功能的一部分。但这不是好的做法。

更新 1.

这是您对渲染的回答 table。

代码

drawTable(data);  /// call this line from your ajax success

function drawTable(data) {
    var tData = data.tblHirarchi
    for (var i = 0; i < tData.length; i++) {
        if (tData[i] != null) {
            drawRow(tData[i])
        }
    }
}

function drawRow(rowData) {
    var row = $("<tr />")
    $("#personDataTable").append(row); //this will append tr element to table... keep its reference for a while since we will add cels into it
    row.append($("<td>" + rowData[0] + "</td>"));
    row.append($("<td>" + rowData[1] + "</td>"));
    row.append($("<td>" + rowData[1] + "</td>"));
}

Fiddle HERE