Я пытаюсь получить данные (список файлов в папке) с моего сервера https://www.my-site.com/api
, где у меня есть index.php
файл, который
<?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);
}
?>
и в моем приложении React я пытаюсь использовать 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));
но результат равен CORS request did not succeed
и CORS header 'Access-Control-Allow-Origin' missing
. Когда я использую Postman, все в порядке, и я получаю json объект с данными, которые я хотел.