[SalesForce] Communication between Lightning Component and Visualforce – DOMException

I'm following the instructions on https://developer.salesforce.com/blogs/developer-relations/2017/01/lightning-visualforce-communication.html to send a message from an iframed VF page to a Lightning component. The problem is I'm getting a DOMException in the console when postMessage is firing.

I tried simplifying the code even more to make sure I didn't have any issues with hard-coded domains.

Here's the Javascript function on my VF Page:

function sendToLC() {
    window.parent.PostMessage("Saved");
}

And here's the function in my Lightning Component controller:

doInit : function(component) {
    window.addEventListener("message", function(event) {
        console.log(event.data);
}, false);

When sendToLC is fired, I get:

LightningDemoRTCHange?id=aAr2F0000008at4SAA:13 Uncaught DOMException: Blocked a frame with origin "https://xxx–daniel–c.cs91.visual.force.com" from accessing a cross-origin frame.

I found Communicating between Lightning component and VF Page, but I don't have development mode on for my user.

Best Answer

Please change the method name to postMessage and postMessage has a second parameter which you need to use. You can use wildcard or put the specific domain (later is better)