简体   繁体   English

axios 没有从 openweathermap api 返回数据

[英]axios is not returning data from openweathermap api

I am trying to get data from openweathermap api.我正在尝试从 openweathermap api 获取数据。 I am easily getting it using postman but I tried with nodejs with axios request and it is responding with 400 status code.我使用邮递员很容易得到它,但我尝试使用带有 axios 请求的 nodejs 并且它以 400 状态代码响应。 I have written following code for it-我为它编写了以下代码-

try {
      let a = 'london'

      const data = await axios.get(
        `api.openweathermap.org/data/2.5/weather?q=${a}&appid=${process.env.WEATHER_API_KEY}`
      )
      console.log('the data is', data)

      if (data) {
        console.log('the data is', data)
        res.status(201).json(data)
      }
    } catch (error) {
      console.log('error', error.message)
      res.status(403)
      res.json(error)
    }

You need to add http:// or https:// to the url.您需要将http://https://到 url。 Without them the URL is incorrecly formatted and axios thinks it's a relative URL (eg /local/api ) but because you try to execute it on node, it doesn't have a context for "relative" as it would in browser (Site origin).没有它们,URL 的格式不正确并且 axios 认为它​​是一个相对 URL(例如/local/api )但是因为您尝试在节点上执行它,它没有像在浏览器中那样的“相对”上下文(站点来源)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM