这是我得到的错误信息:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided
('https://www.youtube.com') does not match the recipient window's origin 
('http://localhost:9000').

我见过其他类似的问题,其中目标源是http://www.youtube.com,收件人源是https://www.youtube.com,但没有一个像我的问题一样,目标是https://www.youtube.com,源是http://localhost:9000。

我不明白这个问题。有什么问题吗? 我该怎么解决呢?


当前回答

我也面临着同样的问题,然后我访问了Youtube官方Iframe Api,在那里我发现了这个:

用户的浏览器必须支持HTML5的postMessage特性。大多数现代浏览器都支持postMessage

而彷徨看到的官方页面也面临着这个问题。只需访问官方Youtube Iframe Api,查看控制台日志。我的Chrome版本是79.0.3945.88。

其他回答

你可以试试:

document.getElementById('your_id_iframe').contentWindow.postMessage('your_message', 'your_domain_iframe')

为了避免控制台错误,我使用了类似Artur之前回答的方法来解决这个问题,以下步骤:

Downloaded the YouTube Iframe API (from https://www.youtube.com/iframe_api) to a local yt-api.js file. Removed the code which inserted the www-widgetapi.js script. Downloaded the www-widgetapi.js script (from https://s.ytimg.com/yts/jsbin/www-widgetapi-vfl7VfO1r/www-widgetapi.js) to a local www-widgetapi.js file. Replaced the targetOrigin argument in the postMessage call which was causing the error in the console, with a "*" (indicating no preference - see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). Appended the modified www-widgetapi.js script to the end of the yt-api.js script.

这不是最好的解决方案(修补本地脚本来维护,失去对消息发送位置的控制),但它解决了我的问题。

在使用此解决方案之前,请参阅此处关于删除targetOrigin URI的安全警告- https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

修复了yt-api.js的例子

我相信这是一个问题与目标来源是https。我怀疑这是因为你的iFrame url使用的是http而不是https。尝试将您要嵌入的文件的url更改为https。

例如:

'//www.youtube.com/embed/' + id + '?showinfo=0&enablejsapi=1&origin=http://localhost:9000';

是:

'https://www.youtube.com/embed/' + id + '?showinfo=0&enablejsapi=1&origin=http://localhost:9000';

至少在我的实例中,这似乎是一个无害的“未准备好”条件,API会不断重试,直到成功为止。

我在任何地方都能找到2到9个(在我最糟糕的测试中,通过蜂窝热点打开了20个标签的2009年的《化石书》)....但之后视频功能正常。一旦它运行我的postmessage基于调用seekTo肯定工作,还没有测试其他。

我的是:

<youtube-player
  [videoId]="'paxSz8UblDs'"
  [playerVars]="playerVars"
  [width]="291"
  [height]="194">
</youtube-player>

我只是用playerVars删除了这一行,它在主机上没有错误。