I'm having a problem getting the different ids from my json object. I get are the id of the last item.
This is the function:
var xmlhttp = new XMLHttpRequest();
var url = "https://wjko5u2865.execute-api.us-east-2.amazonaws.com/articles";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var allart = JSON.parse(this.responseText);
for(let i = 0; i < allart.Items.length; i++)
{
document.getElementById("id").innerHTML = allart.Items[i].id;
}
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
This is the json array I get:
{
"Items":[{
"marca":"Guzzi",
"titolo":"Moto Guzzi V100 Mandello, la regina di EICMA 2021",
"id":"123456",
"immagine":"moto_guzzi_v100_mandello.jpg",
"data":"27/11/2021"
},
{
"marca":"Bimota","titolo":"Bimota: arriverà un'adventure su base Tesi",
"id":"135623",
"immagine":"bimota-_arrivera_unadventure_su_base_tesi.jpg",
"data":"04/12/2021"
},
{
"marca":"Ducati",
"titolo":"Ducati, la DesertX sarà svelata a Dubai il 9 dicembre",
"id":"123789",
"immagine":"b_desertx-dwp2022-2-uc336332-high.jpg",
"data":"04/12/2021"
},
{"marca":"Benelli",
"titolo":"EICMA 2021, Benelli \"sforna\" le moto più attese",
"id":"146975",
"immagine":"benelli_2.jpg",
"data":"27/11/2021"
}
],
"Count":4,"ScannedCount":4}
Thanks to all in advance
document.getElementById("id").innerHTML = ...simply finds the DOM element with ID "id" and writes the content to that same element over and over. That's probably not what you want to do but it's not clear what your HTML contains or what exactly you want to happen.document.getElementById("id").innerHTML += "<br/>" + allart.Items[i].id;