foreach循环的行为不符合预期,javascript ajax调用

时间:2019-07-08 22:11:36

标签: javascript ajax

enter image description here

需要在接下来的14天中每次都找出“ chacesofrain”

只能打印第一天的降雨机会。不确定接下来的几天我该如何得到?

[![$('#weather-click').click(function() {
  var state = $('#state')
    .val()
    .trim();

  var country = $('#country')
    .val()
    .trim();

  var queryURL = `http://api.worldweatheronline.com/premium/v1/weather.ashx?key=[API_KEY]&q=${state},%20${country}&format=json&num_of_days=14`;

  $.ajax({
    url: queryURL,
    method: 'GET'
  }).then(function(response) {
    console.log(response);

    var weather = response.data.weather;
    console.log(weather);
    for (var i = 0; i < weather.length; i++) {
      var hourly = weather[i].hourly;
      console.log(hourly);
      for (var k = 0; k <= hourly.length; k++) {
        var chanceOfRain = hourly[k].chanceofrain;
        console.log(chanceOfRain);
      }

      // setTimeout(function() {
      //   for (var k = 0; k <= hourly.length; k++) {
      //     var changeOfRain = hourly[k].chanceofrain;
      //     console.log(changeOfRain);
      //   }
      // }, 4000);
    }
  });
});][1]][1]

我希望在浏览器中记录的所有日子都充满机会

但是输出仅显示1天

0 个答案:

没有答案