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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:36:06  来源:igfitidea点击:

Get Vimeo thumbnail for video using jQuery

javascriptjquerythumbnailsvimeo

提问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

Please check out this page; Vimeo has a new method call oEmbedas Vimeo is now pushing it's new oEmbed technology.

请查看此页面;Vimeo 有一个新的方法调用,oEmbed因为 Vimeo 现在正在推动它的新 oEmbed 技术。

The method above, will fail on IE (no thumbs will be shown).

上面的方法将在 IE 上失败(不会显示拇指)。

回答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)
});