JavaScript 天气 APIKey 问题

问题描述 投票:0回答:2

代码401

JS代码

我的 API 密钥在浏览器中正常工作。但我无法在实时服务器中获取它。它显示 401 错误为未经授权。我也关闭了防火墙,但也无法获得结果。我的控制台仅显示相同的错误。

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();

javascript api http-status-code-401 weather-api jsapi
2个回答
0
投票

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

使用反引号代替单引号或双引号,

常量响应=等待获取(apiUrl+`&appid=${apiKey}`);


0
投票

您有一个单引号,但您正在使用模板文字,因此您不能使用单引号

应该是这样

const response = await fetch(`${apiUrl}&appid=${apiKey}`);
© www.soinside.com 2019 - 2024. All rights reserved.