Am trying to read a Json file in my react app, but console shows that the url for my json file is 404. Can any one please look into what is wrong in my code
Below is the error shown in browser console. When I open the json url that is shown in network tab, it redirects to the page instead of showing json -
Below is my code -
-
import React from 'react';
import Header from './Header';
import Container from './Container'
import Footer from './Footer'
class App extends React.Component{
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
products: []
};
}
componentDidMount() {
fetch("../json/results.json",{
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(res => res.json())
.then(
console.log("--------"),
(result) => {
this.setState({
isLoaded: true,
products: result.Products
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render(){
const { error, isLoaded, products } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return(
<div>
<Header />
<br></br>
<Container />
<ul>
{(products || []).map(item => (
<li key={item.content_id}>
{item.content_id} {item.content_type}
</li>
))}
</ul>
<br></br>
<Footer />
</div>
);
}
}
}
export default App;
My Json is as below -
{
"Products": [
{
"content_id": "1211122910721",
"content_type": "AVG_Product_C"
]
}
