I'm able to use Postman to fetch this API from Fruityvice. But when I run it locally, I run into CORS issues.
I've been referencing Attempted Solution and Attemped Solution 2 to try and clear the error, but no luck.
Any pointers and where I'm going wrong? Thank you!
Initial Fetch Call
const fetchFruitInformation = async() => {
const response = await fetch("https://www.fruityvice.com/api/fruit/all");
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
};
Error
Access to fetch at 'https://www.fruityvice.com/api/fruit/all' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
So then I add {mode: 'no-cors'}
and also {mode: 'cors}
Updated Fetch Call
const fetchFruitInformation = async() => {
const response = await fetch("https://www.fruityvice.com/api/fruit/all", {
mode: 'no-cors'
});
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
};
Which throws an error
apiCalls.js:7 Uncaught (in promise) Error at fetchFruitInformation (apiCalls.js:7)