How to fetch XML with fetch api

前端 未结 3 1411
说谎
说谎 2020-12-02 22:03

I\'m trying to making a weather app that displays the weather and the temperature of many days of the week. I\'m currently using openweathermap api for such task, the thing

3条回答
  •  忘掉有多难
    2020-12-02 22:50

    Using native DOMParser getCurrentCity(location) can be written:

    function getCurrentCity(location) {
        const lat = location.coords.latitude;
        const lon = location.coords.longitude;
        return fetch(apis.currentWeather.url(lat, lon))
            .then(response => response.text())
            .then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
            .then(data => console.log(data))
    }

提交回复
热议问题