小编Tri*_*ip1的帖子

如何使用fetch api获取XML

我正在尝试制作一个天气应用程序,显示一周中许多天的天气和温度.我目前正在使用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))
Run Code Online (Sandbox Code Playgroud)

javascript xml fetch-api

29
推荐指数
3
解决办法
3万
查看次数

标签 统计

fetch-api ×1

javascript ×1

xml ×1