Using the Yahoo Weather API with JSON and the script tag

£可爱£侵袭症+ 提交于 2019-12-01 21:48:57

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 );
});

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.

Steve Wellens

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 />");
        });

}       
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!