【问题标题】:I get a 404 error when making a Fetch Get Request [closed]发出 Fetch Get 请求时出现 404 错误[关闭]
【发布时间】:2018-09-27 21:50:37
【问题描述】:

我正在尝试从天气 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/”。什么可能导致添加此内容以及如何使此获取请求正常运行?我希望任何人都可以提供任何帮助。

【问题讨论】:

  • 可以在网址中添加http:// 吗?
  • 您的 API 密钥有效吗?
  • 这是“http://”的东西。我只是从 Open Weather Map 复制了 url,并没有想到 http 不存在。谢谢马克!

标签: javascript api get fetch


【解决方案1】:

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

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

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

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

了解更多:Absolute URLs vs relative URLs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多