I’m making a request to cloudinary API from a React & Vite App. I Keep getting these two errors:
Acess to XMLHttpRequest at ‘https://api.cloudinary.com/v1_1/sombra/image/list’ from origin ‘https://7aac-2806-2a0-1400-85b6-f0cb-e8b0-4880-f0ed.ngrok-free.app’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
and
ImageGallery.jsx?t=1688258885347:26 GET https://api.cloudinary.com/v1_1/sombra/image/list net::ERR_FAILED 404 (Not Found) (i checked many times and the cloud name is correct)
This is the component:
import { useEffect, useState } from 'react';
import axios from 'axios';
const ImageGallery = () => {
const [images, setImages] = useState([]);
useEffect(() => {
const fetchImages = async () => {
try {
const response = await axios.get(
'https://api.cloudinary.com/v1_1/sombra/image/list'
);
setImages(response.data.resources);
} catch (error) {
console.error('Error fetching images:', error);
}
};
fetchImages();
}, []);
return (
<div>
<h1>Image Gallery</h1>
<div>
{images.map((image) => (
<img
key={image.public_id}
src={image.secure_url}
alt={image.public_id}
/>
))}
</div>
</div>
);
};
export default ImageGallery;
I tried to make a tunnel since apparently this error is very common and it can be caused by my PC. Also tried to update CORS rules in my vite.config.js file but doesnt seem to work either. This is the code that I’m pasting
server: {
proxy: {
"/api": {
target: "https://api.cloudinary.com/v1_1/sombra/image/list",
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace(/^/api/, ""),
},
},
},
Please help!!
2
Answers
To resolve the CORS issue, you need to configure the server-side to include the appropriate CORS headers in the API response. However, since you are making a request to the Cloudinary API, you don’t have control over the server-side configuration.
Here’s how you can modify your code to use a proxy server:
Hope this would help you.
You need to provide your
API_KEY
,API_SECRET
, and yourcloud_name
which you can find on the dashboard, and replace it with the values in the URL belowHere is a link to the documentation: Get resources from cloudinary