【问题标题】:retiving data from fetch [closed]从 fetch 中检索数据 [关闭]
【发布时间】:2022-01-24 05:48:13
【问题描述】:

JSON 占位符 API 为我提供了数据,但开放的天气 API 给出了错误,错误是“未捕获(承诺中)语法错误:JSON.parse:JSON 数据的第 1 行第 1 列的意外字符” 怎么了?以及如何读取天气 API 数据?


      fetch(
        // 'https://jsonplaceholder.typicode.com/posts/1'
        'api.openweathermap.org/data/2.5/weather?q=bhubneshwar&appid=2f5e9a7699ace605d4cbf50f813d7b0b'
      )
        .then((response) => response.json())
        .then((data) => console.log(data));

【问题讨论】:

  • 在开发工具中简单检查一下请求出了什么问题,就会发现问题。
  • 使用 https 获取返回城市未找到

标签: javascript fetch-api


【解决方案1】:

URL 中缺少 HTTP 协议,此外,您的 URL 上的城市名称不正确。改用它:

https://api.openweathermap.org/data/2.5/weather?q=bhubaneswar&appid=2f5e9a7699ace605d4cbf50f813d7b0b

完整代码:

fetch('https://api.openweathermap.org/data/2.5/weather?q=bhubaneswar&appid=2f5e9a7699ace605d4cbf50f813d7b0b')
   .then((response) => response.json())
   .then((data) => console.log(data));

【讨论】:

  • 好的,对不起。今后我会牢记这一点。 @gre_gor
  • 与城市名错误无关,没有返回有效的JSON。
  • 是的,URL 上也缺少 HTTP 协议。
猜你喜欢
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
相关资源
最近更新 更多