html - 无法在 'postMessage' : Invalid target origin 上执行 'Window'

标签 html iframe sandbox postmessage

我有 iframe:

<iframe id="GameFrame" 
sandbox="allow-scripts allow-pointer-lock" 
src="https://127.0.0.1:112/games/1047/play">
</iframe>

我的父页面位于:

https://127.0.0.1/arcade/not-hidden/space-blaster-1047

我正在尝试向 iFrame 发送消息:

var gameIframe = $("#GameFrame");
gameIframe.get(0).contentWindow.postMessage("screenshot", "");

但这会引发错误:

Uncaught SyntaxError: Failed to execute 'postMessage' on 'Window': Invalid target origin '' in a call to 'postMessage'.

其他尝试:

postMessage("screenshot", "https://127.0.0.1");

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://127.0.0.1') does not match the recipient window's origin ('null').

我怎样才能将此消息发布到 iFrame?

最佳答案

刚想出来,需要用*作为原点:

gameIframe.get(0).contentWindow.postMessage("screenshot", "*");

关于html - 无法在 'postMessage' : Invalid target origin 上执行 'Window',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29918350/

相关文章:

android - 在 Phonegap 应用程序中存储身份验证的正确方法

javascript - 网站应该如何显示其他页面并允许用户对该页面进行评分

具有响应式 Bootstrap 、Vimeo 嵌入和 WordPress 主题的 CSS 1 px 边框

javascript - 直接链接到 iframe 中加载的特定内容的页面

javascript - 限制第三方脚本 - Google Caja?

javascript - 在 Javascript 中围绕 Function() 创建沙箱

php - 在页面底部处理和显示 PHP 错误

javascript - 使用 Javascript 函数将 HTML 写入屏幕

javascript - FancyBox 2.iframe。在表单元素中输入文本时,它会跳转到底部

macos - 在 osx 上的 bazel test env 中运行 docker