【发布时间】:2016-10-08 05:15:16
【问题描述】:
我正在尝试制作一个天气应用程序来显示一周中许多天的天气和温度。我目前正在为此类任务使用 openweathermap api,问题是我想要的信息(即天气日期)仅以 xml 格式提供。 由于我出于学术原因在 ES6(ES2015) 中重建它,因此我也想使用 fetch api,但由于 fetch 方法会解析它,它只会传递一个错误。 那么我该如何获取它或者mby有更好的方法来做到这一点。
let apis = {
currentWeather: { //get user selected recomendation weather
api:"http://api.openweathermap.org/data/2.5/forecast/daily?lat=",
parameters: "&mode=xml&units=metric&cnt=6&APPID=/*api key*/",
url: (lat, lon) => {
return apis.currentWeather.api + lat + "&lon=" + lon +
apis.currentWeather.parameters
}
}
};
function getCurrentLoc() {
return new Promise((resolve, reject) => navigator.geolocation
.getCurrentPosition(resolve, reject))
}
function getCurrentCity(location) {
const lat = location.coords.latitude;
const lon = location.coords.longitude;
return fetch(apis.currentWeather.url(lat, lon))
.then(response => response.json())
.then(data => console.log(data))
}
getCurrentLoc()
.then( coords => getCurrentCity(coords))
【问题讨论】:
标签: javascript xml fetch-api