Using the Yahoo Weather API with JSON and the script tag

拜拜、爱过 提交于 2019-12-02 01:18:45

问题


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.com/forecastjson?w=9807, and I know that the script tag can avoid the same-domain restrictions, but I'm getting a syntax error.

Yahoo's JSON response isn't padded; I've got the callback working but the browser isn't interpreting the JSON properly.

I've seen many examples like How to read yahoo weather JSON data with Jquery ajax but it's so weird because all those give me the cross-domain error.

Can anyone help me with this? Cross domain, yahoo weather, without special servers or YQL or anything like that. Something that just works out of the box.


回答1:


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



回答2:


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.




回答3:


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

}       


来源:https://stackoverflow.com/questions/9237593/using-the-yahoo-weather-api-with-json-and-the-script-tag

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