I am trying to use getStaticProps to simply make a request and then pass that data from it to a component:
But I'm getting this error:
FetchError: invalid json response body at https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR reason: Unexpected token < in JSON at position 0
import AppliancePackage from '../components/AppliancePackage.jsx';
function HomePage({ data }) {
return (
<>
<AppliancePackage appliances={data} />
</>
);
}
export default HomePage;
// This function gets called at build time on server-side.
// It won't be called on client-side, so you can even do
// direct database queries. See the "Technical details" section.
export async function getStaticProps() {
// Call an external API endpoint to get data.
// You can use any data fetching library
var res = await fetch(
'https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR'
);
var json = await res.json();
data = JSON.stringify(json);
console.log('data ', data);
return {
props: {
data: json,
},
};
}
I tried to Stringify it, but that didn't work! Also I am kind of confused by the comments:
This function gets called at build time on server-side. It won't be called on client-side, so you can even do direct database queries. See the "Technical details" section.
And then as you can see there is a comment that states:
Call an external API endpoint to get posts.
But have a whole section regarding API routes in their docs
Anyone can help me what is the matter?
Update
Alexey contributed some great insight, but like I said to him I can't find in the axios docs to change the user-agent!