使用 YouTube iframe API 在本地工作:"Failed to execute 'postMessage' on 'DOMWindow"



我正在在本地服务器上工作,尝试youtube iframe api。

加载iframe时,我会在" domwindow"上获取错误消息"失败"。

如何消除此错误?

我尝试在iframe API页面中开始入门,并能够使用python -m SimpleHTTPServer 8080毫无疑问地运行样本。

您的问题可能与您使用Iframe API的方式有关。检查此信息以获取更多信息。

我认为我们可以自定义yt.player

的sendmessage
playerOptions.playerVars.origin = window.location.origin or your domain.
this.youtubePlayer = new YT.Player(element,playerOptions);
this.youtubePlayer.sendMessage = function (a) {
   a.id = this.id, a.channel = "widget", a = JSON.stringify(a);
   var url = new URL(this.h.src), origin = url.searchParams.get("origin");
   if (origin && this.h.contentWindow) {
       this.h.contentWindow.postMessage(a, origin)
   }
}

我使用此功能解决了我的项目。(原点答案)

相关内容

最新更新