【问题标题】:How to Fix Playback ID Error when using Youtube API to display videos on a webserver?使用 Youtube API 在网络服务器上显示视频时如何修复播放 ID 错误?
【发布时间】:2021-07-25 12:46:01
【问题描述】:

Youtube 播放器正确加载,但视频在点击播放时显示播放错误,并且缩略图也无法加载。有人可以指出我可能做错了什么。

$(document).ready(function(){
    var API_KEY = ""; // Did not include API here but I have it included it my code
    var search = "soccer";
    var video = '';

    videoSearch(API_KEY, search);

    function videoSearch(API_KEY, search){

      $.get("https://www.googleapis.com/youtube/v3/search?key=" + API_KEY
        + "&type=video&part=snippet&maxResults=12&q=" + search, function(data){
          console.log(data);
          data.items.forEach(item => {
            video = `
            <iframe width="420" height="315" src="http://www.youtube.com/embed/$(item.id.videoId)" frameborder="0" allowfullscreen></iframe>
            `
            $("#videos").append(video);
          });
      })
    }
  })

【问题讨论】:

  • 欢迎来到 Stack Overflow。你能用你的代码创建 JSFiddle 吗?并解释问题?

标签: javascript api youtube-api youtube-data-api


【解决方案1】:

尝试用大括号替换冒号。

e.g. $(item.id.videoId) to ${item.id.videoId}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 2021-12-24
    • 2012-08-25
    • 2013-12-08
    • 2015-02-14
    • 2020-06-10
    相关资源
    最近更新 更多