JS:OpenWeatherMap API为参数“rain.3h”返回“undefinded”

时间:2017-05-13 10:00:52

标签: javascript php json api

我想在我的“home.php”中使用此API中的一些参数。 我已经获得了参数cloud,min&的值。最大温度,风等...... 但是在下雨的情况下.3h我的控制台显示错误,因为该点后面的数字为“3”。所以我把它放在括号中,它现在返回“未完成”。你有什么建议可以解决这个问题吗? (为此JSON响应设置了参数rain,因为我在浏览器控制台中看到了该值。 这是我的代码:

function getWeatherData(weatherlat, weatherlong) {
    var url = "http://api.openweathermap.org/data/2.5/forecast?lat=" + weatherlat + "&lon=" + weatherlong + "&units=metric&APPID=88e60f2ce52794ed240874b235f21794";

    $.ajax({
        url: url
    }).done(data => {
        let nextForecast = data.list[0].main;
        let cloudy = data.list[0].clouds;
        let weatherCond = data.list[0].weather[0];
        let windy = data.list[0].wind;
        let rainy = data.list[0].rain;
        console.log(data);

        $('#progressbar2').addClass('hidden');

        //"<img src='http://openweathermap.org/img/w/" + data.weather[0].icon + ".png' alt='Icon depicting current weather.'>"
        document.getElementById("weathercond").innerHTML = `${weatherCond.description}`;
        document.getElementById("weathericon").innerHTML = "<img src='http://openweathermap.org/img/w/" + weatherCond.icon + ".png' alt='Icon depicting current weather.'>"; 
        document.getElementById("cloud").innerHTML = `${cloudy.all}%`;
        document.getElementById("mintemp").innerHTML = `${nextForecast.temp_min}°C`;
        document.getElementById("maxtemp").innerHTML = `${nextForecast.temp_max}°C`;
        document.getElementById("luftftgk").innerHTML = `${nextForecast.humidity}%`;
        document.getElementById("windrtg").innerHTML = `${windy.deg}°`;
        document.getElementById("windspeed").innerHTML = `${windy.speed}m/s`;
        document.getElementById("rainfall").innerHTML = `${['rainy']['3h']}mm`;
    });

0 个答案:

没有答案