javascript - 如何将抓取 YouTube 标题的 jQuery 转换为功能
问题描述
我有以下获取 YouTube 标题的 jQuery 代码。
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id={VIDEOID}&key={YOUR API KEY}&part=snippet&callback=?',function(data){
if (typeof(data.items[0]) != "undefined") {
console.log('video exists ' + data.items[0].snippet.title);
} else {
console.log('video not exists');
}
});
我想将它作为一个函数,以便我可以将 YouTube id 作为参数传递。
解决方案
如果你想把它变成一个函数,这很容易做到:
function youtubeVideo(videoID, apiKey) {
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id='+videoID+'&key='+apiKey+'&part=snippet&callback=?',function(data){
if (typeof(data.items[0]) != "undefined") {
console.log('video exists ' + data.items[0].snippet.title);
} else {
console.log('video not exists');
}
});
}
请注意,我们将 videoID 和 apiKey 传递给函数。为了调用它,您只需:
youtubeVideo(video_id_here, api_key_here);
推荐阅读
- ios - 如何在 Swift 中将 Decodable 与泛型和核心数据一起使用
- url - 为 XenForo 2 创建像这样的友好 URL?
- powershell - 将内容添加到 txt
- wordpress - 如何在 Wordpress 中从视频中添加动画预览
- wso2 - wso2am - 禁用 oauth 同意书
- java - JPA Select查询不返回一个字母单词的结果
- ios - swift 4 删除罢工者
- sql - 在 Postgresql 上的 SQL 查询中在特定条件下增加列值
- javascript - 使用 hichart 绘制折线图
- javascript - 如何在 Node.js 中从 Binance Web 套接字 API 流式传输交易?