Here is the snippet of my fetch json file. There is always an error when I try to fetch the json file data.
const onSearchSubmit = (term) => {
console.log(term);
fetch('./JSONDATA.json', {
method: 'get',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
})
.then(function (response) {
return response.json();
})
.then(function (data) {
setFruits(data);
console.log(data.term);
})
.catch((error) => {
console.log(error + "json file");
});
};
The error is not related to React nor to fetch(). It says your server has responded with 404 Not Found. This means your file is not present / not reachable.
Verify with the browser (or curl) that url http://localhost:3000/JSONDATA.json exists and is reachable
'Content-Type': 'application/json' is not required, this is the request body type header, but you do not have body.