skip to Main Content

Code 401

enter image description here

JS Code

enter image description here

My API key is working properly in the browser. But I can’t get it in the Live server. It shows 401 error as unauthorized. I turned off my firewall too and also I can’t get my results. My console says that same error only.

const apiKey = "5926da84fa5ddbb8fb12992a5d7b7585";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=chennai";
async function checkWeather() {
  const response = await fetch(apiUrl + '&appid=${apiKey}');
  var data = await response.json();
  console.log(data);

}

checkWeather();

2

Answers


  1. const response=await fetch(apiUrl+'&appid=${apiKey}');

    Use backticks instead of single or double quotes,

    const response=await fetch(apiUrl+`&appid=${apiKey}`);

    Login or Signup to reply.
  2. you have a single quote but you are using a template literal so you can’t use a single quote

    so it should be

    const response = await fetch(`${apiUrl}&appid=${apiKey}`);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search