无法在'DOMWindow'上执行'postMessage':https://www.youtube.com!== http:// localhost:9000

 完结那份友谊吧_586 发布于 2022-12-04 13:55

这是我得到的错误消息:

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.

    我没有遇到问题.问题是什么?

    我该如何解决?

Chris Frankl.. 83

我认为这是目标来源的一个问题https.我怀疑这是因为你的iFrame网址正在使用http而不是https.尝试更改您要嵌入的文件的网址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';

不幸的是,https vs http并没有解决我的问题 (67认同)

这个错误有点误导.它实际上与你在`localhost:9000`无关.实际上问题在于你使用youtube-api的方式.当您在代码中将api声明为`tag.src ="https://www.youtube.com/iframe_api";`时,您告诉youtube您想要使用ssl.所以,我建议的更改会改变您使用ssl来请求视频.错误只是说你正在混合ssl和非ssl调用. (26认同)

当我试图在dom中移动iframe时,我遇到了这个问题.`playerEl = document.querySelector('iframe#ytplayer'); anotherEl.appendChild(playerEl); // yt抱怨随后的api电话` (8认同)

同样在这里,它没有解决我的问题...... :-( (4认同)

这似乎奏效了。谢谢!所以问题出在`http`与`https`之间?域名不同(YouTube与本地主机)无关紧要吗?目标原点与接收者原点到底是什么?您说的如何更改收件人的来源(我的URL仍然是localhost:9000)? (2认同)

对于youtube更改为https://,而我的域为https://仍无法解决我的问题。无法在“ DOMWindow”上执行“ postMessage”:提供的目标原点('https://www.youtube.com')与收件人窗口的原点('https://test.dev')不匹配。 (2认同)


小智.. 19

只需在播放器"origin"paramVars属性中添加带有网站网址的参数,如下所示:

this.player = new window['YT'].Player('player', {
    videoId: this.mediaid,
    width:'100%',
    playerVars: { 'autoplay': 1, 'controls': 0,'autohide':1,'wmode':'opaque','origin':'http://localhost:8100' },
}

origin:window.location,在属性中涵盖开发和生产 (5认同)

@JamesBailey`window.location.origin` ..`window.location`是一个对象。 (2认同)


M.Ali El-Say.. 16

设置这似乎解决了它:

  this$1.player = new YouTube.Player(this$1.elementId, {
    videoId: videoId,
    host: 'https://www.youtube.com',

这也为我解决了。也可以这样做:`host:\`$ {window.location.protocol} // www.youtube.com \``, (4认同)

使它`http`(而不是`https`)解决了我的问题. (3认同)


小智.. 8

您可以将JavaScript保存到本地文件中:

https://www.youtube.com/player_api

https://s.ytimg.com/yts/jsbin/www-widgetapi-vfluxKqfs/www-widgetapi.js

player_api将以下代码放入第一个文件中:

if(!window.YT)var YT={loading:0,loaded:0};if(!window.YTConfig)var YTConfig={host:"https://www.youtube.com"};YT.loading||(YT.loading=1,function(){var o=[];YT.ready=function(n){YT.loaded?n():o.push(n)},window.onYTReady=function(){YT.loaded=1;for(var n=0;n

进入第二个文件,找到代码: this.a.contentWindow.postMessage(a,b[c]);

并替换为:

if(this._skiped){
    this.a.contentWindow.postMessage(a,b[c]); 
}
this._skiped = true;

当然,您可以串联到一个文件中-效率更高。这不是一个完美的解决方案,但是可以!

我的资料来源:yt_api-concat

4 个回答
  • 设置这似乎解决了它:

      this$1.player = new YouTube.Player(this$1.elementId, {
        videoId: videoId,
        host: 'https://www.youtube.com',
    

    2022-12-11 02:09 回答
  • 只需在播放器"origin"paramVars属性中添加带有网站网址的参数,如下所示:

    this.player = new window['YT'].Player('player', {
        videoId: this.mediaid,
        width:'100%',
        playerVars: { 'autoplay': 1, 'controls': 0,'autohide':1,'wmode':'opaque','origin':'http://localhost:8100' },
    }
    

    2022-12-11 02:57 回答
  • 您可以将JavaScript保存到本地文件中:

    https://www.youtube.com/player_api

    https://s.ytimg.com/yts/jsbin/www-widgetapi-vfluxKqfs/www-widgetapi.js

    player_api将以下代码放入第一个文件中:

    if(!window.YT)var YT={loading:0,loaded:0};if(!window.YTConfig)var YTConfig={host:"https://www.youtube.com"};YT.loading||(YT.loading=1,function(){var o=[];YT.ready=function(n){YT.loaded?n():o.push(n)},window.onYTReady=function(){YT.loaded=1;for(var n=0;n<o.length;n++)try{o[n]()}catch(i){}},YT.setConfig=function(o){for(var n in o)o.hasOwnProperty(n)&&(YTConfig[n]=o[n])}}());
    

    进入第二个文件,找到代码: this.a.contentWindow.postMessage(a,b[c]);

    并替换为:

    if(this._skiped){
        this.a.contentWindow.postMessage(a,b[c]); 
    }
    this._skiped = true;
    

    当然,您可以串联到一个文件中-效率更高。这不是一个完美的解决方案,但是可以!

    我的资料来源:yt_api-concat

    2022-12-11 03:12 回答
  • 我认为这是目标来源的一个问题https.我怀疑这是因为你的iFrame网址正在使用http而不是https.尝试更改您要嵌入的文件的网址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';
    

    2022-12-11 03:13 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有