我在 PHP 中有一个函数,它返回这样的 JSON:
{"tableRow":[ {"tipo":"Noche"},{"patente":"XZ7410"},{"nombre":"Marcela Bien"},
{"revisado":0},{"id_registro":"10"},
{"tipo":"Vespertino"},{"patente":"EW3651"},{"nombre":"Alexis Diaz"},
{"revisado":1},{"id_registro":"9"} ]
}
也有这个表与一些默认创建的 HTML 内容:
<table class="table table-condensed table-striped table-hover table-bordered pull-left" id="data-table">
<thead>
<tr>
<th>Turno</th>
<th>ID Máquina</th>
<th>Operador</th>
<th>Semáforo</th>
<th> </th>
</tr>
</thead>
<tbody id="data-update">
<tr>
<td>Noche</td>
<td>XZ7410</td>
<td>Marcela Bien</td>
<td><img src="/monitor/web/images/bullet_red.png" /></td>
<td><a class="btn btn-success btn-mini" href="/monitor/web/frontend_dev.php/ver-detalles/10">Ver detalles</a></td>
</tr>
<tr>
<td>Vespertino</td>
<td>EW3651</td>
<td>Alexis Diaz</td>
<td><img src="/monitor/web/images/bullet_orange.png" /></td>
<td><a class="btn btn-success btn-mini" href="/monitor/web/frontend_dev.php/ver-detalles/9">Ver detalles</a></td>
</tr>
</tbody>
</table>
我想用从 PHP 生成的内容更新 #data-update HTML 内容,并将其作为 JSON 提供给 jQuery,所以我编写了以下代码:
$(document).ready(function() {
setInterval(function() {
$.ajax({
type: 'get',
url: '<?php echo url_for('dashboard/BuildAjax') ?>',
dataType: 'json',
success: function(data) {
var newRows;
for (var i in data.tableRow) {
newRows += "<tr><td>" + data.tableRow[i].tipo + "</td>"
newRows += "<td>" + data.tableRow[i].patente + "</td>"
newRows += "<td>" + data.tableRow[i].nombre + "</td>"
newRows += "<td>" + data.tableRow[i].revisado + "</td>"
newRows += "<td>" + data.tableRow[i].id_registro + "</td></tr>"
}
$("#data-update").html(newRows);
},
error: function() {
alert('<?php echo __('Error al cargar los datos') ?>');
}
});
}, 10000);
});
HTML 内容已更改,但方式不正确。这是什么意思?结果,我得到了一张这样的表格:
Turno ID Maquina Operador Semaforo
Noche undefined undefined undefined
undefined XZ7410 undefined undefined
undefined undefined Marcela Bien undefined
undefined undefined undefined 1
undefined undefined undefined undefined 10
Vespertino undefined undefined undefined
undefined EW3651 undefined undefined
undefined undefined Alexis Diaz undefined
undefined undefined undefined 0
undefined undefined undefined undefined 12
什么时候应该得到类似的东西:
Turno ID Maquina Operador Semaforo
Noche XZ7410 Marcela Bien 1 10
Vespertino EW3651 Alexis Diaz 0 12
怎么了?我找不到我犯错的地方,有什么帮助或建议吗?