使用 getJSON 和 YQL 时出现问题

发布于 2024-11-14 18:17:44 字数 685 浏览 1 评论 0原文

我正在尝试通过 YQL 获取 Youtube 中视频的观看次数以将其显示在我的网站上,我正在使用 jQuery 的 $.getJSON 方法来获取 YQL 调用的结果,我做得很好但由于某种原因,成功部分没有触发,这是我的代码:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrWlHtvZHbZ8%26feature%3Dplayer_embedded%22%20and%0A%20%20%20%20%20%20xpath%3D'%2Fhtml%2Fbody%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B2%5D%2Fdiv%2Fspan%2Fstrong'&format=json&diagnostics=true&callback=cbfunc",
    function(data) {
        alert('Hello');
    }
);

我在 firebug 的控制台上看到请求已发出,并且它返回正确的 json 对象,但我的函数中的警报没有触发,我做错了什么这里?

提前致谢!

I'm trying to get the views of a video in Youtube through YQL to display it on my site, I'm using jQuery's $.getJSON method to get the result of the YQL call which I do fine but for some reason the success part isn't firing, here's my code:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrWlHtvZHbZ8%26feature%3Dplayer_embedded%22%20and%0A%20%20%20%20%20%20xpath%3D'%2Fhtml%2Fbody%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B2%5D%2Fdiv%2Fspan%2Fstrong'&format=json&diagnostics=true&callback=cbfunc",
    function(data) {
        alert('Hello');
    }
);

I see on my firebug's console that the request is made and it's returning the correct json object but the alert in my function isn't firing, what am I doing wrong here?

Thanks in advance!

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

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

发布评论

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

评论(3

九厘米的零° 2024-11-21 18:17:44

从 URL 中删除 &callback=cbfunc,以便 jQuery 可以自己定义回调。

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrWlHtvZHbZ8%26feature%3Dplayer_embedded%22%20and%0A%20%20%20%20%20%20xpath%3D'%2Fhtml%2Fbody%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B2%5D%2Fdiv%2Fspan%2Fstrong'&format=json&diagnostics=true",
    function(data) {
        alert('Hello');
    }
);

http://jsfiddle.net/niklasvh/C5cYB/

Remove &callback=cbfunc from the URL, so jQuery can define the callback itself.

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrWlHtvZHbZ8%26feature%3Dplayer_embedded%22%20and%0A%20%20%20%20%20%20xpath%3D'%2Fhtml%2Fbody%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B3%5D%2Fdiv%2Fdiv%2Fdiv%5B2%5D%2Fdiv%2Fspan%2Fstrong'&format=json&diagnostics=true",
    function(data) {
        alert('Hello');
    }
);

http://jsfiddle.net/niklasvh/C5cYB/

我是有多爱你 2024-11-21 18:17:44

使用 jQuery 的匿名回调被认为是不好的做法,请参阅 小心 YQL 博客上“帮助”您的客户端库

由于您的原始 YQL URL 告诉 YQL 需要一个名为 cbfunc 的回调函数,因此您需要做的就是在 JavaScript 中定义该函数(在 window 范围内)。

function cbfunc(data) {
    alert('Hello');
}

It is considered bad practice to use jQuery's anonymous callbacks, see Beware of client-side libraries "helping" you on the YQL blog.

Since your original YQL URL told YQL to expect a callback function called cbfunc, then all you need to do is define that function in your JavaScript (in the window scope).

function cbfunc(data) {
    alert('Hello');
}
盛夏已如深秋| 2024-11-21 18:17:44

我对 YQL 一无所知,但我看到在该查询字符串末尾设置了显式回调,这可能是一个问题。您是否尝试过删除“callback = cbfunc”,或添加“callback =?”如 http://api.jquery.com/jQuery.getJSON/ 的 jsonp 文档中所示

I know nothing about YQL, but I see an explicit callback set at the end of that querystring, which could be a problem. Have you tried removing "callback=cbfunc", or putting "callback=?" as in the jsonp documentation at http://api.jquery.com/jQuery.getJSON/

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