I want to populate HTML form input fields with JSON data I get from my API. I can display the data in a table with the following code, but I cannot make it work with a form. I tried to address the form with something like or instead of and , but it doesn’t work. Does anyone know if I can transform this code to work with a form?
const url = "/api/contacts/" + localStorage.getItem("paraID");
fetch(url).then((res) => {
res.json().then((data) => {
console.log(data);
if (data.length > 0) {
var temp = "";
data.forEach((itemData) => {
temp += "<tr>";
temp += "<td>" + itemData.studentID + "</td>";
temp += "<td>" + itemData.nachname + "</td>";
temp += "<td>" + itemData.studium + "</td>";
temp += "<td>" + itemData.semester + "</td>";
temp += "<td>" + itemData.deaktiviert + "</td></tr>";
});
document.getElementById("myTable").innerHTML = temp;
}
});
});
The fetch function works, I can see the correct JSON data in the console. It looks like this:
[{…}]
0: {studentID: 1, vorname: ‘Marie’, nachname: ‘Bauer’, strasse: ”, plz: ”, …}
length: 1
[[Prototype]]: Array(0)
I tried all the different options I found at stackoverflow (e.g. with jQuery), but they also don’t work with a form.
2
Answers
This is how I got it to work:
let = exampleData
exampleData.forEach((itemData) …
withdata.forEach((itemData) …
you can try this code: