【发布时间】:2014-06-14 16:52:39
【问题描述】:
我找到了一个很好的参考来获取给定 Youtube 视频 ID 的 youtube 视频标题,但我不确定如何在他提供的函数中返回标题。在 videoInfoCallback 函数中我让它返回消息但我不确定是什么被传递到 info 参数中。有什么建议。这是我正在使用的资源:@987654321@
这是我正在玩的代码:
function registerScript(url) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = url;
document.getElementsByTagName('head')[0].appendChild(s);
}
function videoInfoCallback(info) {
if (info.error) {
alert('Error\n\n' + info.error.message);
} else {
var message = info.data.title;
return message;
}
return "hello";
}
function getVideoInformation(id) {
if (id) {
return registerScript('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=jsonc&callback=videoInfoCallback');
} else {
alert('Please enter an id.');
}
}
基本上,我想要一个函数来接收像 CFF0mV24WCY 这样的 Youtube 视频 id 并让它返回标题。
【问题讨论】:
-
您可以在浏览器中使用调试器并在回调函数中设置断点来查看
info包含的内容。 -
好的,我知道内容是什么,但它只是一个包含所有视频信息的对象。这是在做什么?v=2&alt=jsonc&callback=videoInfoCallback'?
-
它们是记录在 here 的 YouTube API 的查询参数。
v是 API 的版本,alt是返回数据的格式,callback是返回数据后触发的回调函数。
标签: javascript jquery html json youtube