无法在表格中将结果显示为JSON结果

时间:2019-01-27 16:12:23

标签: javascript jquery json ajax

我对JQuery的ajax方法具有成功的功能。 我可以正常工作并且有响应,但是问题是试图在表中显示数组。

success:function(resp){
            var json =JSON.parse(JSON.stringify(resp));
            console.log(json); 
            $("#table").append('<tr><td>Titulo</td>'+
            '<td>Consola</td>' + 
            '<td>Precio</td>');
            for (i=0;i<json.length; i++){
               $("#tbody").append('<tr>' + 
               '<td align="center" style="display: none;">' + json[i].titulo + '</td>'+
               '<td align="center" style="display: none;">' + json[i].consola + '</td>'+
               '<td align="center" style="display: none;">' + json[i].precio + '</td>'+'</tr>');
               } //cierre del for.
            $('#modal').click();
            }

我的html是:

 <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Lista de juegos disponibles</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
            <table class="table" id="table">
    </table>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
        <button type="button" class="btn btn-primary">OK</button>
      </div>
    </div>
  </div>
</div>

,console.log中的数组为:

[Array(2)]
0: Array(2)
0: {titulo: "Kingdom Hearts 2.5 HD remix", consola: "2", precio: "15990"}
1: {titulo: "Tales of Xillia 2", consola: "2", precio: "14990"}
length: 2
__proto__: Array(0)
length: 1
__proto__: Array(0)

1 个答案:

答案 0 :(得分:0)

@天使戈麦斯

tr是在tbody中创建的,您可以使用以下代码来实现相同的目的。

for (i=0;i<json.length; i++){
               $("#table>tbody").append('<tr>' + 
               '<td align="center" style="display: none;">' + json[i].titulo + '</td>'+
               '<td align="center" style="display: none;">' + json[i].consola + '</td>'+
               '<td align="center" style="display: none;">' + json[i].precio + '</td>'+'</tr>');
               }
相关问题