是否可以中止Google API请求?就像是:
req = gapi.client.request({
path: 'youtube/v3/search',
params: {
q: search_query,
type: 'video',
part: 'id,snippet',
maxResults: 8,
fields: 'items(id(videoId),snippet(thumbnails,title))'
})
req.execute(function(result){ console.log(result) })
然后再(在请求返回之前)
req.abort()
最佳答案
最后,我通过简单地手工构建XMLHttpRequest(使用jQuery)来完成此操作。这非常容易。
window.current_search = $.ajax('https://content.googleapis.com/youtube/v3/search', {
dataType: 'jsonp',
data: {
q: search_query,
type: 'video',
part: 'id,snippet',
maxResults: 8,
fields: 'items(id(videoId),snippet(thumbnails,title))',
key: apiKey
}
}).done(render_page)
如果我需要取消请求...
window.current_search.abort()
该代码甚至没有比使用该API的版本更长。