地下十天天气预报



我试图在我的loadd10dayforecast函数下面得到一个10天的预测,但我得到以下错误:

Cannot read property 'simpleforecast' of undefined

似乎我不知道正确的api调用基于此:http://www.wunderground.com/weather/api/d/docs?d=data/forecast10day

这是奇怪的,因为我能够得到当前条件显示正确。

  //Get the current weather conditions
    function getCurrentConditions(text) {
    console.log("n")
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
        if (doc.readyState == XMLHttpRequest.DONE) {
            var jsonObject = eval('(' + doc.responseText + ')');
            loadedCurrentConditions(jsonObject);
        }
    }
    doc.open("GET", "http://api.wunderground.com/api/KEY/forecast/geolookup/conditions/q/"+ text + ".json");
    doc.send();
}
//Get the 10 Day Forcast
function get10DayForcast(text) {
    console.log("n")
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
        if (doc.readyState == XMLHttpRequest.DONE) {
            var jsonObject = eval('(' + doc.responseText + ')');
            loaded10DayForcast(jsonObject);
        }
    }
    doc.open("GET", "http://api.wunderground.com/api/KEY/forecast10day/q"+ text + ".json");
    doc.send();
}
//Display to console
function showRequestInfo(text) {
    console.log(text)
}

function loadedCurrentConditions(jsonObject)
{
    showRequestInfo("Current Temp F:" + jsonObject.current_observation.temp_f);
    showRequestInfo("Weather Description:" + jsonObject.current_observation.weather);
    showRequestInfo("Icon:" + jsonObject.current_observation.icon);
}
function loaded10DayForcast(jsonObject)
{
    showRequestInfo("Today High Temp F:" + jsonObject.forecast.simpleforecast.forecastday.Forecast[0].low.fahrenheit);
}

在您的代码示例中,您有一个名为" loadd10dayforecast "的方法,因为您正在尝试访问JSON树中不存在的对象。

我认为你想这样做:

…jsonObject.forecast.simpleforecast.forecastday [0] .low.fahrenheit…

// JavaScript Document
jQuery(document).ready(function($) {
  /* Edit these variables */
  var api = "api key";
  var state = "Ks";
  var city = "McLouth";
// JavaScript Document  
  $.ajax({
  url: "http://api.wunderground.com/api/" + api + "/forecast10day/conditions/q/" + state + "/" + city + ".json",
  dataType : "jsonp",
  success : function(parsed_json) {
      console.log(parsed_json);
         for(var some in parsed_json.forecast.txt_forecast.forecastday){
         console.log("*************");
          $("#nxt10Days").append("
<b>"+parsed_json.forecast.txt_forecast.forecastday[some].title+" </b> <br />"+
 parsed_json.forecast.txt_forecast.forecastday[some].fcttext+"<br />");                              
 console.log(parsed_json.forecast.txt_forecast.forecastday[some].title);
      }
    }
  });
});
接下来的10天,

最新更新