I'm trying to make a fetch request to a local json file and I get this error saying Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0 pointing at response.json(). I tried adding proxy package.json but i get 500 internal error. How can I solve this?
componentDidMount() {
fetch('http://localhost:3000/items.json')
.then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response");
}
return response.json();
})
.then(function(data) {
console.log(data)
});
}
json file:
[
{
data: [
{
name: "cnt",
level: "12"
},
{
name: "stewart",
level: "6"
},
{
name: "nic",
level: "7"
}
]
}
]
import obj from './items.json'. If you put the file in thepublicfolder, you will be able to get it from the same origin:fetch('/items.json')fetch('/items.json')worked in public folder.