0

我尝试使用以下代码按照他们的文档获取 vine 缩略图:

<!-- language: lang-js -->
var onGetVineThumbnailSuccess = function( videoUrl ) {
    return function( response ) {
    var args = { videoUrl: videoUrl };
    args.thumbnailUrl = response['thumbnail_url']; // jshint ignore:line

    $rootScope.$broadcast( 'event:onGetVineThumbnailSuccess', args);
  };
};

var getVineThumbnail = function ( videoUrl ) {
  $http
    .get( 'https://vine.co/oembed.json?url=' + encodeURIComponent( videoUrl ) )
    .then( onGetVineThumbnailSuccess( videoUrl ) );
};

但在控制台中我有这个错误:

XMLHttpRequest cannot load https://vine.co/oembed.json?url=https%3A%2F%2Fvine.co%2Fv%2FeV1mMuab7Mp. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

顺便说一句,这个链接:https://vine.co/oembed.json?url=https%3A%2F%2Fvine.co%2Fv%2FeV1mMuab7Mp有效。如果我直接放到浏览器的网址栏。我得到这个JSON

{
  "version": 1.0,
  "type": "video",
  "cache_age": 3153600000,
  "provider_name": "Vine",
  "provider_url": "https://vine.co/",
  "author_name": "Evengelia",
  "author_url": "https://vine.co/u/1204040590484971520",

  "title": "Everything was beautiful on this day. #6secondsofcalm",

  "thumbnail_url": "https://v.cdn.vine.co/r/videos/59734161E81269170683200901120_45a46e319ea.1.1.8399287741149600271.mp4.jpg?versionId=tc3t.oqGtjpJNlOX1AeM1CAnWONhbRbQ",
  "thumbnail_width": 480,
  "thumbnail_height": 480,
  "html": "<iframe class=\"vine-embed\" src=\"https://vine.co/v/eV1mMuab7Mp/embed/simple\" width=\"600\" height=\"600\" frameborder=\"0\"><\/iframe><script async src=\"//platform.vine.co/static/scripts/embed.js\"><\/script>",
  "width": 600,
  "height": 600
}

听起来像 CORS 问题。但是由于我无法控制Vine,我应该如何调用此服务?

4

1 回答 1

0

Access-Control-Allow-Origin设置在来自服务器的响应上,而不是在客户端请求上,以允许来自不同来源的客户端访问响应。

在您的情况下,http://www.vine.co/不允许您的来源访问响应。因此,您无法阅读它。

有关 CORS 的更多信息:https ://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

但是,Chrome Webstore 有一个扩展程序,当页面中有异步调用尝试访问与您的主机不同的主机时,它会为您添加“Access-Control-Allow-Origin”标头。

扩展名是:“Allow-Control-Allow-Origin:*”,这是链接:https ://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

于 2015-10-30T12:16:37.907 回答