问题描述
有时我在通过json提取youtube视频时遇到此错误,这是一个示例:
Sometimes I get this error when extracting youtube videos by json, here's an example:
XMLHttpRequest无法加载 https://gdata.youtube. com/feeds/api/users/IcarusTouma?& alt = json . Access-Control-Allow-Origin不允许使用来源 http://localhost .
XMLHttpRequest can not load https://gdata.youtube.com/feeds/api/users/IcarusTouma?&alt=json. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
我尝试使用jsonp,但youtube不接受jsonp.
I tried with jsonp, but youtube does not accept jsonp.
因此,请从youtube中提取视频.
So extract videos from youtube.
$. getJSON ('https://gdata.youtube.com/feeds/api/videos/' + id_video + '? & alt = json', function (data) {
if (typeof data! == "undefined" && data)
{
var title = data ['entry'] ['title'] ['$ t'];
var thumb = data ['entry'] ['media $ group'] ['media $ thumbnail'] [0] ['url'];
var title = data ['entry'] ['author'] [0] ['name'] ['$ t'];
$. getJSON ('https://gdata.youtube.com/feeds/api/users/' + author + '? & alt = json', function (data) {
author_thumbnail var = data ['entry'] ['media $ thumbnail'] ['url'];
$ ('.items').append(html_carousel (thumb, title, author_thumbnail, author, 5,' youtube ', id_video, author'));
});
}
});
推荐答案
刚刚检查了YouTube数据API参考文档,对于JSON和较新的JSON-C格式,它们都接受JSON-P,因此您可以指定通过callback
GET字符串参数进行回调.该链接将证明这是真的:
Having just checked the YouTube Data API reference documentation, for both JSON and the newer JSON-C formats, they both accept JSON-P, allowing you to specify the callback by the callback
GET string parameter. This link will demonstrate that this is true:
https://gdata.youtube.com/feeds/api/videos?q=baseball&v=2&alt=jsonc&callback=functionName
在这种情况下,长JSON对象返回包装在对functionName
的函数调用中.下一个URL是相同的调用,但使用的是旧的(且已过时)JSON格式:
The long JSON object return is wrapped in the function call to functionName
, in this case. The next URL is the same call, but in the older (and somewhat deprecated) JSON format:
https://gdata.youtube.com/feeds/api/videos?q=baseball&v=2&alt=json&callback=functionName
数据有些不同,但结果集相同,包装在另一个对functionName
的调用中.现在,这些调用是查询,用于获取与搜索参数(棒球")匹配的视频.但我敢肯定,他们会仔细阅读他们的参考文档(对于 JSON-C 和 JSON 格式)将产生您所需的内容,以使其能够正常工作,作为JSONP调用,并指定了回调在查询字符串中.
The data is a bit different, but same result set, wrapped in another call to functionName
. Now, these calls were queries to get the videos that matched the search param ("baseball"). But I'm sure perusing their reference documents (for the JSON-C and JSON formats) will yield what you need to make this work properly, as a JSONP call, specifying the callback in the query string.
这篇关于无法从youtube通过json提取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!