我在PHP函數返回一個像這樣的JSON:與AJAX表格中的「TBODY」更改HTML內容調用
{"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>
我想更新#數據更新HTML內容與PHP中生成的一個分給jQuery的作爲JSON,所以我寫了這個代碼:
$(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
有什麼不對?我找不到我犯的錯誤,有什麼幫助或建議?
是我有點與JSON tryng擺脫PHP正確的版本,我測試你的代碼,並將其與工作stucked少數黑客;) – Reynier