Youtube 您需要刷新搜索列表api才能获得响应
下面是我的JSP页面Youtube 您需要刷新搜索列表api才能获得响应,youtube,youtube-api,Youtube,Youtube Api,下面是我的JSP页面 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <script src="search.js" type="text/javascript"></script> <script src="https://apis.google.com/j
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="search.js" type="text/javascript"></script>
<script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<div id="buttons">
<label> <input id="query" type="text"/><button id="search-button" onclick="search1()">Search</button></label>
</div>
<pre id="response"></pre>
</body>
</html>
第一次登录页面时,我能够得到响应,但当我输入搜索输入新查询并尝试搜索时,除非再次刷新页面,否则不会给出任何输出。这与onClientLoad()方法有关。如何克服这一问题
function showResponse(response) {
var responseString = JSON.stringify(response, '', 2);
document.getElementById('response').innerHTML += responseString;
}
// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
}
// Called automatically when YouTube API interface is loaded (see line 9).
function onYouTubeApiLoad() {
gapi.client.setApiKey('myapi');
//search1();
}
function search1() {
// Use the JavaScript client library to create a search.list() API call.
//onClientLoad();
var q = $("#query").val();
var request = gapi.client.youtube.search.list({
q: q,
part: 'snippet'
});
// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
}
// Called automatically with the response of the YouTube API request.
function onSearchResponse(response) {
showResponse(response);
}