node.js - axios 没有从 openweathermap api 返回数据
问题描述
我正在尝试从 openweathermap api 获取数据。我很容易使用邮递员获得它,但我尝试使用带有 axios 请求的 nodejs 并且它以 400 状态代码响应。我为此编写了以下代码-
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)
}
解决方案
您需要在网址中添加http://
或。https://
没有它们,URL 的格式不正确,axios 认为它是一个相对 URL(例如/local/api
),但是因为您尝试在节点上执行它,所以它没有像在浏览器中那样的“相对”上下文(站点来源)。
推荐阅读
- java - 没有找到合适的云连接器 IntelliJ 与在 Eclipse 中工作时出错
- python - 对列进行标记后,获取特定单词前后的 2 个单词
- python - 如何在 Python 的不同机器上找到相同的文件夹?
- python - Python - 遍历大列表并快速下载图像
- android - 我需要在 Flutter 中使用 Firebase/Google 移动广告的互联网权限吗
- android - 持久的底部导航栏,但仅在某些页面中
- regex - 正则表达式替换为模式但保留模式本身
- c# - .NET 5 - 添加 JwtBearer 作为默认架构时登录停止工作
- amazon-ec2 - Unable to connect to Snowflake using snowsql on EC2
- python - How can I get the index of a specific element in a Spinner from Python's Kivy library?