I try to fetch data (list of files in folder) from my server https://www.my-site.com/api where I've got index.php file which is
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$dir = "./photos"; //path
$list = array(); //main array
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) != false){
if($file == "." or $file == ".."){
//...
} else {
$list3 = array(
'url' => $file);
array_push($list, $list3);
}
}
}
$return_array = array('photos'=> $list);
echo json_encode($return_array);
}
?>
and in my React app i try to use fetch
fetch('https://www.my-site.com/api', {
method: 'GET',
redirect: 'follow',
headers: {
}
})
.then(response => response.json())
.then(res => console.log(res))
.catch(error => console.log('error', error));
but the result is CORS request did not succeed and CORS header 'Access-Control-Allow-Origin' missing. When im using Postman, everything it's ok, and I get json object with data I wanted.