我正在开发一个社区视频网站(Wordpress 主干网),该网站将允许用户简单地提交一个链接并拥有该网站上的所有信息。我的主要部分正在工作(过滤视频 ID 并将值植入 Javascript 函数中以获取观看次数)。我正在尝试获取视频的上传者,但由于某种原因,它似乎无法正常工作(在我的其他网站上工作,?)。
这是我的代码片段:
假设视频 ID 为“3DdQw4w9WgXcQ”
//Here is the player (separate from JS, used PHP)
<iframe width="802" height="480" src="http://www.youtube.com/embed/3DdQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>
//Here is where I want the uploader to display, if the function doesn't execute correctly, it displays a common message, "Error"
<div id="postedby">Error</div>
<script type="text/javascript">
function youtubeVidCallback( data )
{
document.writeln( '<h3><strong>' + addCommas( data.entry[ "yt$statistics" ].viewCount ) );
$('#postedby').html( 'Posted by' + data.entry[ "author" ][ 0 ].name.$t );
}
</script>
//This next part loads YouTube's feed and gets the script rolling
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/3DdQw4w9WgXcQ?v=2&alt=json-in-script&callback=youtubeVidCallback"></script>
我的目标是启用此功能,以便我可以将订阅链接返回到视频作者的 channel ,并可能加载他们的头像。
最佳答案
你有正确的想法,但你不应该对 url 进行编码:
<script type="text/javascript" src="https://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=json-in-script&callback=youtubeVidCallback"></script>
fiddle :
http://jsfiddle.net/EvbZm/
关于javascript - 使用 Javascript 从 YouTube Vid 中获取信息(代码需要改进),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7924005/