I am trying to make a client-side get request to this api:
'api.openweathermap.org/data/2.5/weather?zip='
here we need to pass zip+apiKey to get a response:
I am doing this in the browser and I'm getting response successfully:
here in the app trying to do that but I am getting error in the 'get request' that the url or the get route is wrong:
/* Global Variables */
let baseURL = 'api.openweathermap.org/data/2.5/weather?zip=';
let apiKey = '####';
let appID='&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;
const getTemp = async (baseURL, temperature, appID,apikey)=>{
const res = await fetch(baseURL+temperature+appID+apikey)
try {
const data=await res.json();
return data;
} catch(error) {
console.log("error", error);
// appropriately handle the error
}
}
getTemp().then( res =>{ console.log(res);
postData('/add', {temperature: temperature, date: newDate, userResponse: userResponse});
updateUI('/all');})
https://codesandbox.io/s/romantic-leaf-0bvpd?file=/src/index.html