In React, I use the fetch function to receive a JSON file with the following content. However, if the result array has more than 3 elements, I encounter an error. Where could the issue be?
Content of the JSON file:
{
"result": [
{
"open": 2428700,
"high": 2442100,
"low": 2427600,
"close": 2439100,
"change": 169000,
"changePercent": 0.7,
"date": "2023-11-25",
"datePersian": "1402/09/04"
},
{
"open": 2415500,
"high": 2427600,
"low": 2414200,
"close": 2422200,
"change": 78000,
"changePercent": 0.32,
"date": "2023-11-23",
"datePersian": "1402/09/02"
},
{
"open": 2421600,
"high": 2430300,
"low": 2413500,
"close": 2414400,
"change": -78000,
"changePercent": -0.32,
"date": "2023-11-22",
"datePersian": "1402/09/01"
},
{
"open": 2392700,
"high": 2428000,
"low": 2392700,
"close": 2424300,
"change": 212000,
"changePercent": 0.88,
"date": "2023-11-21",
"datePersian": "1402/08/30"
},
{
"open": 2423200,
"high": 2423200,
"low": 2387600,
"close": 2403100,
"change": -210000,
"changePercent": -0.87,
"date": "2023-11-20",
"datePersian": "1402/08/29"
}
]
}
React fetch:
fetch('https://app.takhminzan.com/data.json')
.then(response => response.json())
.then(data => {
console.log(data.result);
})
.catch(error => {
console.error('Error:', error);
});
Error:
Access to fetch at ‘https://app.takhminzan.com/data.json’ 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.
I changed the hosting to address this issue and removed access restrictions in the .htaccess file. However, the problem still persists.
Content of .haccess file:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
</IfModule>