我正在尝试将 react url 作为 iframe 加载到我的 jsp 项目中。
这是我的发送方代码块:
<iframe id="eda"
style="display: none;"
src="http://myhost:3000/"
width="100%" height="600" border="0" marginwidth="0"
marginheight="0" scrolling="no">
</iframe>
****
function loadReactIframe(){
document.getElementById("eda").contentWindow.postMessage('GET MESSAGE FROM ME', '*');
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了以下方法:
function loadReactIframe(){
document.getElementById("eda").contentWindow.postMessage(
'GET MESSAGE FROM ME',
'http://myhost', 3000
);
}
Run Code Online (Sandbox Code Playgroud)
我的接收器(反应)代码块:
componentDidMount() {
window.addEventListener('load', this.handleLoad);
alert('componentDidMount')
}
handleLoad(event) {
alert(event.data);
}
Run Code Online (Sandbox Code Playgroud)
但我无法从事件中获取数据。