Using the Yahoo Weather API with JSON and the script tag

前端 未结 3 1545
自闭症患者
自闭症患者 2021-01-20 05:53

I\'m trying to get the Yahoo Weather with JavaScript. I originally made a proxy, but found that clumsy.

So can get the JSON response from http://weather.yahooapis.co

3条回答
  •  被撕碎了的回忆
    2021-01-20 06:20

    If you want to use yql, this is the link:

    http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%223015%22&format=json

    When you call it just pass that as the parameter in your jquery. So, in other using STeve's code you can simply replace the url passed into the getJSON function call with the yql link and of course replace the zip code you want to use for the location. So, here's the code:

        $(document).ready(DocReady);
    
    function DocReady()
    {
        var Result = $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20location%3D%2233015%22&format=json", "",
        function (data)
        {
            $("body").append("Sunrise: " + data.query.results.channel.astronomy.sunrise + "
    "); $("body").append("SuntSet: " + data.query.results.channel.astronomy.sunset + "
    "); }); }

    Here is the section you need to replace to get the proper location:

    Enter zip code between both %22's

    http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20location%3D%22

    33333

    %22&format=json

    Let me know if you have any questions.

提交回复
热议问题