Я столкнулся с проблемой, связанной с API openweathermap, вызов API отлично работает в браузере, но при использовании его в проекте я получаю ошибку 401, которая упоминается здесь следующим образом:
у меня нет ни одной из перечисленных проблем
также вот ссылка: http://api.openweathermap.org/data/2.5/weather?zip=10001,us&appid=####
отлично работает в браузере
и вот мой app.js:
/* Global Variables */
let baseURL = 'http://api.openweathermap.org/data/2.5/weather?zip=';
let apiKey = ',us&appid=####';
// Create a new date instance dynamically with JS
let d = new Date();
let newDate = d.getMonth()+'.'+ d.getDate()+'.'+ d.getFullYear();
document.getElementById('generate').addEventListener('click', performAction);
function performAction(e){
const temperature = document.getElementById('zip').value;
const userResponse = document.getElementById('feelings').value;
console.log(baseURL+temperature+apiKey)
const getTemp = async (baseURL,temperature,apiKey)=>{
const res = await fetch(baseURL+temperature+apiKey)
try {
const data=await res.json();
return data;
} catch(error) {
console.log("error", error);
// appropriately handle the error
}
}
getTemp(baseURL+temperature+apiKey).then( res =>{ console.log(res);
postData('/add', {temperature: temperature, date: newDate, userResponse: userResponse});
updateUI('/all');})
когда я console.log() URL-адрес, он работает отлично, но в запросе на получение появляется ошибка: