I have this json file I try to read using JS but it returns a syntax error: unexpected token at line 2 ":"
Where could this error come from?
fetch('../json/destinations_data.json')
.then(response => response.text())
.then(data => {
window.alert(data);
console.log(data);
});
{
"user1":{
"email":"lucien.bramare@gmail.com",
"password":"oss117",
"prenom":"Lucien",
"nom":"Bramare"
},
"user2":{
"email":"noel.flantier@gmail.com",
"password":"oss117",
"prenom":"Noël",
"nom":"Flantier"
}
}
You're parsing it as text, not JSON.
fetch('../json/destinations_data.json')
.then(response => response.json()) //<-- do this!
.then(data => {
window.alert(data);
console.log(data);
});
Try to parse it as json and not as text
replace this
.then(response => response.text())
with this
.then(response => response.json())
You are reading json file so instead of using response.text()
use response.json()
fetch('../json/destinations_data.json')
.then(response => response.json())
.then(data => {
window.alert(data);
console.log(data);
});
There is a syntax error in your json file. You have to change your format. Please check https://www.w3schools.com/js/js_json.asp
users = [
{
"email":"lucien.bramare@gmail.com",
"password":"oss117",
"prenom":"Lucien",
"nom":"Bramare"
},
{
"email":"noel.flantier@gmail.com",
"password":"oss117",
"prenom":"Noël",
"nom":"Flantier"
}
];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.