I am creating an application that will list all of the names, ages, Genders, on a web page. The data returned by the server is in JSON format. Everything looks fine, but when the Show Family Info button is clicked, it gives me this error:
SyntaxError: Unexpected token o in JSON at position 1
After getting that error, I went to my JSON file and checked to see if there is any error that I might have, but I find none. There is no "o" in the first position.
So I'm not sure where the error came from.
Here is my HTML code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
Fetch API
</title>
</head>
<body>
<p id="demo">
</p>
<button onclick="showFamilyInfo()">
Show Family Info
</button>
<script>
let arrayForData = [];
function showFamilyInfo(){
fetch('users.json')
.then(function(response){
response.json()
.then(function(data){
let parseData = JSON.parse(data);
parseData.forEach(function(element){
for(let i of parseData[element]){
arrayForData.push(`'
<p>
${i}: ${i.FirstName}
${i}: ${i.LastName}
${i}: ${i.Age}
${i}: ${i.Gender}
</p>
'`);
}
});
document.getElementById('demo').innerHTML = arrayForData.join();
})
.catch(function(error){
document.getElementById('demo').innerHTML = error;
});
});
}
</script>
</body>
</html>
And here is my JSON code:
[
{
"FirstName":"Toby",
"LastName":"Harnish",
"Age":17,
"Gender":"Male"
},
{
"FirstName":"Kelly",
"LastName":"Harnish",
"Age":51,
"Gender":"Female"
},
{
"FirstName":"Dale",
"LastName":"Harnish",
"Age":57,
"Gender":"Male"
},
{
"FirstName":"Avery",
"LastName":"Harnish",
"Age":23,
"Gender":"Male"
},
{
"FirstName":"Annie",
"LastName":"Harnish",
"Age":16,
"Gender":"Female"
},
{
"FirstName":"Benjimin",
"LastName":"Harnish",
"Age":16,
"Gender":"Male"
},
{
"FirstName":"Wesley",
"LastName":"Harnish",
"Age":16,
"Gender":"Male"
},
{
"FirstName":"Clara",
"LastName":"Harnish",
"Age":14,
"Gender":"Female"
},
{
"FirstName":"Bobby",
"LastName":"Harnish",
"Age":15,
"Gender":"Male"
},
{
"FirstName":"Olivia",
"LastName":"Harnish",
"Age":20,
"Gender":"Female"
},
{
"FirstName":"Timmy",
"LastName":"Harnish",
"Age":19,
"Gender":"Male"
}
]