Remplir un tableau html avec le résultat [JSON] d'une requête ajax
Bonjour à tous,
Je viens de débuter JQuery et je cherche à remplir un tableau html avec le résultat (format json) d'une requete ajax. J'essaie avec une boucle for mais à la fin j'obtiens uniquement le dernier élément du résultat json alors que j'aurais voulu qu'il m'affiche les uns après les autres dans le tableau. Merci par avance ! Voici mon bout de code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var index = 0;
$(document).ready(function(){
console.log("ready !")
$.ajax({
type: "GET", //rest Type
dataType: 'json', //mispelled
url: "http://localhost:8080/v1/log/student/blondelt",
async: true,
contentType: "application/json; charset=utf-8",
success: function (msg) {
for(var index=0; index<msg.length;index++){
$("#username").html(msg[index].username);
$("#exercise").html(msg[index].exercise);
$("#computer").html(msg[index].computer);
$("#content").html(msg[index].content);
}
}
});
}) |
Et le code html du tableau
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <table class="table table-striped" id="display">
<tr>
<th>Username</th>
<th>Exercise</th>
<th>Machine</th>
<th>Contenu</th>
</tr>
<td id = "username">username</td>
<td id = "exercise">exercise</td>
<td id = "computer">computer</td>
<td id = "content">content</td>
</table> |