javascript 使用 jQuery 获取视频的 Vimeo 缩略图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4709601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Vimeo thumbnail for video using jQuery
提问by Lee Tindell
I've found similar questions but none of the answers show clearly and easily how to get a thumbnail for a vimeo video using jQuery and JSON. If anyone can help that would be great, here is what I've got but it shows nothing at the moment.
我发现了类似的问题,但没有一个答案清楚且轻松地显示如何使用 jQuery 和 JSON 获取 vimeo 视频的缩略图。如果有人可以提供帮助,那就太好了,这就是我所拥有的,但目前没有显示任何内容。
var vimeoVideoID = '17631561';
var videoCallback = 'showThumb';
$.getJSON('http://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=' + videoCallback,
function(data){
$(".thumbs").attr('src',data[0].thumbnail_large);
});
Thanks in advance.
提前致谢。
回答by Lance
I believe you're having the "same origin policy" issue. You should consider writing a server side script using something like "file_get_contents" or "fopen", enabling you to grab the data from vimeo, translate it to json, and output to your javascript with a nice ajax call.
我相信您遇到了“同源政策”问题。您应该考虑使用诸如“ file_get_contents”或“ fopen”之类的内容编写服务器端脚本,使您能够从 vimeo 获取数据,将其转换为 json,并通过一个不错的 ajax 调用输出到您的 javascript。
If you would like to avoid using a server-side script you may use the data type JSONP.
如果您想避免使用服务器端脚本,您可以使用数据类型 JSONP。
var vimeoVideoID = '17631561';
$.getJSON('https://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=?', {format: "json"}, function(data) {
$(".thumbs").attr('src', data[0].thumbnail_large);
});
Notice the URL is a bit different from how you are using it. The callback which you defined as a var is unnecessary. You're attaching the getJSON to a function directly, so you'll call the 'callback' in the url '?'. This informs the getJSON function to pass the successful data return to the supplied function.
请注意,该 URL 与您使用它的方式略有不同。您定义为 var 的回调是不必要的。您将 getJSON 直接附加到一个函数,因此您将在 url '?' 中调用'回调'。这会通知 getJSON 函数将成功的数据返回传递给提供的函数。
You can test my code here. Hope it helps!
你可以在这里测试我的代码。希望能帮助到你!
回答by Stefan Bracke
回答by Brian T
With the updated API, it would be...
使用更新的 API,它将是......
$.getJSON('https://vimeo.com/api/oembed.json?url=https://vimeo.com/' + id, {format: "json"}, function(data) {
$(".thumbs").attr('src', data.thumbnail_url)
});

