管搜索列表api需要刷新才能获得响应

时间:2016-09-06 10:23:37

标签: 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/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>

我的search.js

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

我第一次登陆页面时能够得到响应,但是当我进入搜索输入一个新查询并尝试搜索它时不会给出任何输出,除非我再次刷新页面。这与onClientLoad()mehtod有关。我怎么能克服这个?

0 个答案:

没有答案