将 Yahoo Weather API 与 JSON 和脚本标记结合使用

发布于 2025-01-04 17:33:04 字数 526 浏览 1 评论 0 原文

我正在尝试使用 JavaScript 获取雅虎天气。我最初做了一个代理,但发现很笨拙。

因此可以从 http://weather.yahooapis.com/forecastjson?w=9807 获取 JSON 响应,我知道脚本标记可以避免同域限制,但我收到语法错误。

雅虎的 JSON 响应未填充;我的回调已正常工作,但浏览器无法正确解释 JSON。

我见过很多例子,例如如何读取雅虎天气JSON 数据与 Jquery ajax 但这很奇怪,因为所有这些都给了我跨域错误。

谁能帮我解决这个问题吗?跨域、雅虎天气,无需特殊服务器或 YQL 或类似的东西。开箱即用的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宣告ˉ结束 2025-01-11 17:33:04

如果您需要 JSON-P,那么您需要向查询添加回调函数名称。对于 jQuery,这始终是 ?。 jQuery 将用随机生成的函数名称替换它:

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'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 );
});
尘世孤行 2025-01-11 17:33:04

如果您想使用 yql,请使用以下链接:

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

当你调用它时,只需将其作为 jquery 中的参数传递即可。因此,在其他使用 STeve 的代码中,您可以简单地将传递到 getJSON 函数调用的 url 替换为 yql 链接,当然还可以替换您想要用于该位置的邮政编码。因此,这是代码:

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

}

这是您需要替换以获得正确位置的部分:

在两个 %22 之间输入邮政编码

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

33333

%22& format=json

如果您有任何疑问,请告诉我。

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.

烟雨扶苏 2025-01-11 17:33:04

这是一些代码

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

}       

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

}       
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文