【问题标题】:the openwheather api is not working in react js while url showing data in browser当 url 在浏览器中显示数据时,openwheather api 在 react js 中不起作用
【发布时间】:2021-08-18 21:48:56
【问题描述】:
import React, { Component } from 'react'
import './App.css';
import axios from 'axios'
const api=axios.create({
  baseURL:'https://api.openweathermap.org/data/2.5/weather?q=Karachi&appid=e3f8864274e67280b3ee8eb501d3718a'
})



export class App extends Component {
  constructor(){
    super();
    api.get('/').then(res=>{
      console.log(res.data)
    })
  }
  render() {
    return (
      <div className="App">
       <h1>React Weather App</h1>
       <main>
       </main>
       <footer>
       </footer> 
      </div>
    )
  }
}

export default App

以上代码与wheather app相关。 url 在浏览器中显示数据但不工作并显示错误:xhr.js:177 GET https://api.openweathermap.org/data/2.5/weather?q=Karachi&appid=e3f8864274e67280b3ee8eb501d3718a/401 (Unauthorized)

【问题讨论】:

    标签: reactjs axios


    【解决方案1】:

    这个错误 401 (Unauthorized) 是因为你的“appid”错误或过期。

    查看此页面了解更多信息:https://openweathermap.org/appid

    【讨论】:

    • 解决办法是什么?
    • 查看此页面了解更多信息:openweathermap.org/appid
    • 但是使用类似的 appid(上面在代码中提到)我在浏览器中得到了正确的结果。这意味着我认为 appid 是正确的。
    • 我不知道该 API,但如果您收到此错误(401(未授权))不是正确的密钥或您无权访问该资源。请检查此以了解 401 错误:developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
    • 查看这个页面,这个FAQ页面:openweathermap.org/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2013-09-07
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    相关资源
    最近更新 更多