我已經編寫了一個小型移動Web應用程序,以通過手機在我的PC上控制YouTube,但使用YouTube API進行搜索時發生了一些奇怪的事情。第一次加載頁面時,一切都很好 - 輸入搜索詞,單擊搜索並返回結果。Youtube API僅在第一頁訪問時加載
但是,如果我點擊另一個頁面然後回來,搜索不再起作用,並且我在下面的搜索功能中看到「Uncaught TypeError:無法讀取'search'undefined」的屬性。
我對JavaScript很陌生,所以請隨時反饋代碼,但我已經看到這個問題一段時間了,儘管大量的谷歌搜索一直沒能找到解決方案。
// Called automatically when JavaScript client library is loaded.
function onClientLoad()
{
//
try
{
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
}
// Called automatically when YouTube API interface is loaded.
function onYouTubeApiLoad()
{
gapi.client.setApiKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}
function search(q) {
// Create api request and execute it
var request = gapi.client.youtube.search.list({
type: 'video',
part: 'snippet',
q: q
});
// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
}
function onSearchResponse(response) {
showResponse(response);
}
鏈接到API的腳本是我search.aspx頁面如下圖所示:也被用於
<script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
JQuery的,所以我不知道是否有變形產生的任何不道德的行爲但在這一點上,任何想法都將非常感謝!
如果client.js是服務器上的本地文件(/ lib/client.js),那麼看起來回調參數將被忽略:http://stackoverflow.com/a/37303942/988591。 –