1

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: enter image description here

I am doing this in the browser and I'm getting response successfully: enter image description here

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

mai mohamed
  • 105
  • 5
  • 19

1 Answers1

0

Here your are missing one thing 'http' protocol as browser automatically adds that. In your code make these changes:

let baseURL = 'http://api.openweathermap.org/data/2.5/weather?zip=';
let apiKey = '####';
let appID='&appid='
Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35