2014-11-15 3 views
0

사용자가 현재 위치 (즉, 리즈, 맨체스터, 런던 등)를 입력 할 수있게하려고합니다. 현재 내가 현재 위치를 알려주려고합니다. 그들이 입력 한 지역의 임시 직원, 그러나 나는 아무것도 표시 할 수 없습니다. 내가 뭔가 잘못하고 있는거야?Wunderground Api 사용자가 자신의 위치를 ​​입력하여 위치를 알려줍니다.

<h3> Weather Data </h3> 
<form method="POST" action="about.php"> 
    <!-- user inputs their location here --> 
    <input type="text" name="location" value= "Country"/> 
    <input type="text" name="city" value='City'/> 
    <input type="submit" name="submit" value="submit" /> 
</form> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 

<script> 
jQuery(document).ready(function($) { 
    $.ajax({ 
     url : "http://api.wunderground.com/api/5e8af95dbdebbd73/geolookup/conditions/forecast/q/UK/England.json", 
     dataType : "jsonp", 
     success : function(parsed_json) { 
      var location = parsed_json['location']['city']; 
      var temp_f = parsed_json['current_observation']['temp_f']; 
      alert("Current temperature in " + location + " is: " + temp_f); 
     } 
    }); 
}); 
</script> 

스크립트 위 wunderground에 대한 API 호출이 당신이 여기입니다 호출이 반환하는 개체 중 하나라고 생각 구조를 반환하지 않습니다 임시 기능

답변

0

을한다.

{ 
city: "Aberdaron", 
country: "UK", 
country_iso3166: "GB", 
country_name: "United Kingdom", 
l: "https://stackoverflow.com/q/zmw:00000.1.03405", 
name: "Aberdaron", 
state: "", 
zmw: "00000.1.03405" 
} 

날씨 데이터가 없습니다.

날씨 데이터에 적합한 API 호출을받을 때이 코드가 도움이 될 것입니다.

See the working Demo

(function() { 
    var city_data; 
    // don't wait for the document to load to get the city data. 
    (function() { 
     $.ajax({ 
      url: /* the same from the question */, 
      dataType: "jsonp", 
      success: function (parsed_json) { 
       // build the map in a tmp variable as we don't want 
       // city_data to be partially initialized. 
       var tmp = {}; 
       $.each(parsed_json.response.results, function (key, val) { 
        // map the city name to its data 
        tmp[val.city] = val; 
       }); 
       city_data = tmp; 
      } 
      // todo handle errors 
     }); 
    }()); 

    jQuery(document).ready(function ($) { 

     // on page load register the on form submit call back 
     $('form').submit(function (e) { 
      e.preventDefault(); 

      // the api call has not finished yet 
      if (city_data === undefined) { 
       alert("Sorry no city data yet"); 
       return; 
      } 

      // get the value in the text box 
      var city = $('[name="city"]', this).val(); 
      if (city_data.hasOwnProperty(city)) { 
       alert(city_data[city].country_name); 
      } else { 
       alert(city + " was not found"); 
      } 
     }); 
    }); 
}()); // execute immediately 
관련 문제