【问题标题】:In need of assistance with Yahoo Weather API需要雅虎天气 API 的帮助
【发布时间】:2015-03-10 02:19:53
【问题描述】:

我很难将 Yahoo API 用于天气应用程序。我试图让它显示天气信息,用于小规模测试,只是风寒。

index.html

<head>
<title>Alex Webber - MyWeather</title>
<script language="javascript" type="text/javascript" src="js/weather">     </script>
</head>
<body>
<p id="demo"><script>weather();</script></p>
</body> 

weather.js

function weather(){
var callbackFunction = function(data) {
var wind = data.query.results.channel.wind;
document.getElementById("demo").innerHTML = wind.chill; 
};
}

【问题讨论】:

  • 你的问题是什么?发生错误?
  • @Raptor 很抱歉之前没有提到这一点。我想我在这里忘记了一些东西gist.githubusercontent.com/ydn/6ef5a695e871b8a628d0/raw/…
  • 没有显示当前的风寒,不知道把丢失的信息放在哪里。
  • 在浏览器中检查开发者控制台。是否提示任何 JS 错误?网络请求是否通过成功?
  • @Raptor windchill 未定义

标签: javascript yahoo-api yahoo-weather-api


【解决方案1】:

您必须调用 QUERY 到 yahoo 天气 api,您似乎忽略了它。 查询现在设置为 * 以便它可以返回除风之外的其他数据。加上一个变量 var location 被分配到查询中以覆盖所有区域。使用美国地区的州名,例如。城市中的纽约,乡村中的纽约。

<!DOCTYPE html>
<html>
    <head>
        <script>

            function getLocation() {
               var city = document.getElementById("city").value;
               var country = document.getElementById("country").value;
               var location = city + "," + country;

                //create a script and add it to your page
                var script = document.createElement('script');
                script.src = "https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + location + "')&format=json&callback=callbackFunction";
                document.getElementsByTagName('head')[0].appendChild(script);

                }


                var callbackFunction = function(data) {
                var wind = data.query.results.channel.wind;
                var wind_chill = wind.chill;
                var wind_speed = wind.speed;
                var wind_direction = wind.direction
                document.getElementById("chill").innerHTML = wind_chill;
                document.getElementById("speed").innerHTML = wind_speed;
                document.getElementById("direction").innerHTML = wind_direction;
                //alert(wind.chill);
            }
        </script>     

    </head>
    <body>
        City:<input type="text" id="city">
        Country/State in U.S.A<input type="text" id="country">
        <input type="button" onclick="getLocation()" value="Get Weather">
        <p>Wind Chill</p>
        <p id="chill"></p>
        <p>Wind Speed</p>
        <p id="speed"></p>
        <p>Wind Direction</p>
        <p id="direction"></p>
    </body>
</html>

贡献者 Brian Glaz 帮我解决了这个问题,向我展示了如何将 var 位置分配给查询以及如何在输入数据后调用查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多