使用 getJSON 和 YQL 时出现问题
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 URL 中删除
&callback=cbfunc
,以便 jQuery 可以自己定义回调。http://jsfiddle.net/niklasvh/C5cYB/
Remove
&callback=cbfunc
from the URL, so jQuery can define the callback itself.http://jsfiddle.net/niklasvh/C5cYB/
使用 jQuery 的匿名回调被认为是不好的做法,请参阅 小心 YQL 博客上“帮助”您的客户端库。
由于您的原始 YQL URL 告诉 YQL 需要一个名为
cbfunc
的回调函数,因此您需要做的就是在 JavaScript 中定义该函数(在window
范围内)。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 thewindow
scope).我对 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/