我正在尝试做一个 Youtube API,我觉得除了这个gapi和res之外我一切都正常?它说gapi未定义。我怎样才能做到这一点?
function tplawesome(e,t){res=e;for(var n=0;n<t.length;n++){res=res.replace(/\{\{(.*?)\}\}/g,function(e,r){return t[n][r]})}return res}
$(function() {
$("form").on("submit", function(e) {
e.preventDefault();
// prepare the request
var request = gapi.client.youtube.search.list({
part: "snippet",
type: "video",
q: encodeURIComponent($("#search").val()).replace(/%20/g, "+"),
maxResults: 3,
order: "viewCount",
publishedAfter: "2015-01-01T00:00:00Z"
});
// execute the request
request.execute(function(response) {
var results = response.result;
$("#results").html("");
$.each(results.items, function(index, item) {
$.get("tpl/item.html", function(data) {
$("#results").append(tplawesome(data, [{"title":item.snippet.title, "videoid":item.id.videoId}]));
});
});
resetVideoHeight();
});
});
$(window).on("resize", resetVideoHeight);
});
function resetVideoHeight() {
$(".video").css("height", $("#results").width() * 9/16);
}
function init() {
gapi.client.setApiKey("AIzaSyD646m4ZfK5yKBZj9p95LohN-PTUnRHBRY");
gapi.client.load("youtube", "v3", function() {
});
}
gapi
是由 Google API javascript 库创建的对象,它为您管理所有交互(即执行所有繁重的请求)。如果未定义该对象,则您可能没有将库本身包含在页面中。在 HTML 中的某个位置,您需要一个脚本标记来加载位于以下位置的库:
https://apis.google.com/js/client.js https://apis.google.com/js/client.js
请注意,在使用脚本标记加载库时,您还应该向其传递一个回调...这是一个在库加载完成后将自动调用的函数。因此,在您的情况下,您的 init() 方法就是该回调,因此您的脚本标记将如下所示:
<script src="https://apis.google.com/js/client.js?onload=init"></script>
浏览器将获取库,加载它,然后在库加载完成后运行 init() ,并且所有内容都将准备好在触发时执行您的表单。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)