我在发出 Fetch Get 请求时收到 404 错误 [已关闭]

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

我正在尝试从天气 api 获取一些天气信息。当我在 Chrome 的控制台中加载此获取代码时,出现 404 错误。

const getWeather = () => {
  return fetch('api.openweathermap.org/data/2.5/weather? 
q=London&appid=' + apiKey)
  .then(response => response.json())
  .then(weather => console.log(JSON.stringify(weather)))
}

getWeather();

它还显示了“http://127.0.0.1:5500/”在我尝试获取的网址之前。什么可能导致添加此内容以及如何使此获取请求正常运行?我希望任何人都能提供任何帮助。

javascript get fetch-api
1个回答
0
投票

您使用的网址被视为相对网址(因此浏览器会在其前面加上您网站的当前位置)。要使其成为绝对网址,您可以使用

'https://api.openweathermap.org/data/2.5/weather?q=London&appid=' + apiKey

'//api.openweathermap.org/data/2.5/weather?q=London&appid=' + apiKey

最后一个是使用与您的网站运行相同的协议。

了解更多:绝对 URL 与相对 URL

© www.soinside.com 2019 - 2024. All rights reserved.