при попытке прочитать данные с закрытой зоны удаленного хоста получаем ошибку 401.
JS file... (находится на localhost)
let api_url = 'https://xxxxx/xxx.php'
let user = 'xxxxx'
let passwd = 'xxxxxx'
fetch(api_url, {
method: 'post',
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true,
'Authorization': 'Basic ' + btoa(user + ":" + passwd),
'Content-Type':'application/x-www-form-urlencoded'
},
mode: 'no-cors',
})
.then(response=>response.json())
.then(json => console.log(json))
.catch(error=> console.log(error))
На удаленном хосте: https://xxxxx/xxx.php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
echo '{"status":"ok"}'; exit;
Хостинг руцентр
Вопрос, в чем ошибка? Почему выплывает шибка: POST https://xxxx/xxx.php net::ERR_ABORTED 401
Если на localhost в JS убираем "mode: 'no-cors'" то получаем другую ошибку:
Access to fetch at 'https://xxxx/xxx.php' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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.