【问题标题】:How to fectch API in React JS?如何在 React JS 中获取 API?
【发布时间】:2021-09-05 06:33:00
【问题描述】:

我正在尝试从 OpenWeather API 获取数据。根据文档,响应是 JSON。但是我收到以下错误:

SyntaxError: Unexpected token < in JSON at position 0

useEffect(() => {
fetch(`api.openweathermap.org/data/2.5/weather?q=${CITY_NAME}&appid=${API_KEY}`)
.then(
  (res)=> res.json()
  .then((data)=>{
    console.log(data)
  })
  .catch((error)=>{
    console.log(error)
  })
)
}, [])

当我查看开发人员工具时,我还有一个状态码 304:未修改。

很少有问题与我的问题相匹配,但没有一个可以解决我的问题,例如: Fetching JSON returns error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

【问题讨论】:

  • @yudhiesh 是的!有效

标签: json reactjs api


【解决方案1】:

API 的路径不正确,应该是 http://api.openweathermap.org/data/2.5/weather?q=${CITY_NAME}&amp;appid=${API_KEY} 代替。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多