我有 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/