Using the Yahoo Weather API with JSON and the script tag

前端 未结 3 1546
自闭症患者
自闭症患者 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 + "<br />");
            $("body").append("SuntSet: " + data.query.results.channel.astronomy.sunset + "<br />");
        });
    
    }
    

    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.

    0 讨论(0)
  • 2021-01-20 06:30

    If you're expecting JSON-P then you need to add a callback function name to the query. With jQuery, this is always ?. jQuery will substitute it with a randomly generated function name:

    var query = escape('select item from weather.forecast where location="CAXX0518"'),
        url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=?"; 
    
    $.getJSON(url, function(data) {
      console.log( data );
    });
    
    0 讨论(0)
  • 2021-01-20 06:31

    Here is some code

    $(document).ready(DocReady);
    
    function DocReady()
    {
        jQuery.support.cors = true;
        var Result = $.getJSON("http://weather.yahooapis.com/forecastjson?w=9807", "",
            function (data)
            {
                $("body").append("Sunrise: " + data.astronomy.sunrise + "<br />");
                $("body").append("SuntSet: " + data.astronomy.sunset + "<br />");
            });
    
    }       
    
    0 讨论(0)
提交回复
热议问题