如何从具有不同域的iFrame发送帖子消息?

Yin*_*non 5 javascript iframe reactjs

我正在尝试与父窗口和iFrame之间的帖子进行通信。这些框架位于不同的域(我的域和Google Maps iFrame)中。

从父窗口向iFrame发送发布消息可以正常工作:

const iFrameNode = ReactDOM.findDOMNode(this.refs.iframe);
iFrameNode.contentWindow.postMessage({data: JSON.stringify(data), type: 'GET_MARKERS'}, '*');
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试向父窗口发送回复时:

window.parent.postMessage({
    type: 'MARKERS',
    data: {
        mapInstance: window.googleMapsInstance,
        markersInstance: window.googleMapsMarkerInstance
    }
}, '*')
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Uncaught DOMException: Blocked a frame with origin "https://localhost" from accessing a cross-origin frame.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?