简体   繁体   English

无法获取json对象

[英]Can't get json object

I am trying to get city name from input element and then get the weather information and also timezone information of the city from API. 我想从输入元素中获取城市名称,然后从API获取城市的天气信息以及时区信息。 However i can't get the json object in that function but it works outside the function. 但是我无法在该函数中获取json对象,但它在该函数之外起作用。 Can anyone help me? 谁能帮我?

 var d = "Lisbon"; $(document).ready(function() { $("#button").click(function() { d = document.getElementById("input").value; $(document).ready(function myfunction() { $.getJSON('http://api.openweathermap.org/data/2.5/weather?APPID=11d324c05e02e308c1c9eb8d8041b143&q=' + d + '&units=metric', function(data) { document.getElementById("city").innerHTML = data.name + ", " + data.sys.country; document.getElementById("wind").innerHTML = data.wind.speed + " m/s "; document.getElementById("cloudiness").innerHTML = data.weather[0].description; document.getElementById("pressure").innerHTML = data.main.pressure + " hpa"; document.getElementById("humidity").innerHTML = data.main.humidity + " %"; document.getElementById("coord").innerHTML = "[ " + data.coord.lat + ", " + data.coord.lon + " ]"; document.getElementById("temperature").innerHTML = data.main.temp + "°C"; document.getElementById("icon").innerHTML = "<img src=http://openweathermap.org/img/w/" + data.weather[0].icon + ".png" + ">"; console.log(data); (function() { $.getJSON("http://api.geonames.org/timezoneJSON?" + "lat=" + data.coord.lat + "&" + "lng=" + data.coord.lon + "&username=hrrs", function(data) { document.getElementById("time").innerHTML = data.time; document.getElementById("timeCity").innerHTML = data.timezoneId; document.getElementById("country").innerHTML = data.countryName; document.getElementById("sunrise").innerHTML = data.sunrise; document.getElementById("sunset").innerHTML = data.sunset; console.log(data); }); })(); }); document.getElementById("slayt").innerHTML = '<iframe src="http://www.panoramio.com/wapi/template/slideshow.html?tag=' + d + '&amp;width=1920&amp;height=500&amp;" frameborder="0" width="1920" height="500" scrolling="no" marginwidth="0" marginheight="0"></iframe>'; })(); }); }); 

Is this what you are trying to achieve? 这是您要达到的目标吗? http://jsfiddle.net/wnunhhac/ http://jsfiddle.net/wnunhhac/

<input type="text" id="inputCity" value="Zurich"/>
<input type="button" id="start" value="Go" />   
<br />
<span id="city">click go</span>

$(document).ready(function() {
    $("#start").click(function() {
      var d = $("#inputCity").val();
      $.getJSON('http://api.openweathermap.org/data/2.5/weather?APPID=11d324c05e02e308c1c9eb8d8041b143&q=' + d + '&units=metric', function(data) {
        $("#city").html(data.name);          
      });
    });
});

The API returns status code 404 not found, if the city is invalid. 如果城市无效,API将返回找不到状态码404。 So you should code an appropriate resonse. 因此,您应该编码适当的共振。

if (data.cod === "404") {
  // Api could not find city
  return;
}

Also I changed the button type to "button" instead of "submit" 我也将按钮类型更改为“按钮”,而不是“提交”

<button type="button" id="button" class="btn btn-default">

Corresponding Fiddle: http://jsfiddle.net/xvn6f05w/ 对应的小提琴: http : //jsfiddle.net/xvn6f05w/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM