I’m getting a CORS error from a dockerized ReactJS app. When I tested it locally it worked fine. But after I dockerized it and pushed it to Docker Desktop as port 3003, it gave an error:
Access to XMLHttpRequest at ‘https://keto-diet.p.rapidapi.com/?protein_in_grams__lt=15&protein_in_grams__gt=5’ from origin ‘http://localhost:3003’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header has a value ‘http://localhost:3000’ that is not equal to the supplied origin.
Here is my React code:
const options = {
method: "GET",
url: "https://keto-diet.p.rapidapi.com/",
params: {
protein_in_grams__lt: '15',
protein_in_grams__gt: '5'
},
headers: {
"X-RapidAPI-Key": "myapikeyishere",
"X-RapidAPI-Host": "keto-diet.p.rapidapi.com",
},
};
const Products = () => {
const [products, setProducts] = useState([]);
const ketoRecipes = async () => {
const response = await axios.request(options);
setProducts(response.data);
};
useEffect(() => {
ketoRecipes();
}, []);
Here is the docker desktop
What should I do to avoid the CORS issues? Thanks
2
Answers
If you don’t control the API server-because it’s a third-party API- you can try to use proxy.
I assume the server is expressjs
to call this API, you can use minimalized url like
Add the following in the options
This will allow the json format file transfer.