【问题标题】:Open weather api when using metrics in link getting error在链接中使用指标时打开天气 api 出现错误
【发布时间】:2017-06-16 07:14:55
【问题描述】:
fetch(`http://api.openweathermap.org/data/2.5/forecast/weather?q=${SendForm.cityName}&units=metric&APPID=******************`)
.then(res => res.json())
.then(results => {
console.log(results);
});
当我在 url 中没有这部分时,它会正常拉取对象
units=metric
但是有了它我得到了
Object
cod: "0"
message: "Error"
【问题讨论】:
标签:
javascript
json
api
ecmascript-6
openweathermap
【解决方案1】:
显然你的 API 调用有错误:
http://api.openweathermap.org/data/2.5/forecast/weather?q=London,uk&units=metric&APPID=***
无论如何都会给出:
{"cod":"400","message":"strconv.ParseInt: parsing \"weather\": invalid syntax"}
因为forecast/weather 应该是forecast 或weather。
如果您想检索 current weather,请按照 OWM 文档中记录的正确 API 调用:
api.openweathermap.org/data/2.5/weather?q={city name},{country code}
或5 day / 3 hour forecast data:
api.openweathermap.org/data/2.5/forecast?q={city name},{country code}
修复了这个问题,&units=metric 似乎工作正常。