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

前端 未结 6 1436
长发绾君心
长发绾君心 2020-12-05 09:47

I have a game in heroku, now I\'m trying to make it work in Facebook canvas, but, while it works in Firefox, in Chrome and IE doesn\'t.

IE shows a warning with a but

相关标签:
6条回答
  • 2020-12-05 10:17

    My issue was I was instatiating the player completely from start but I used an iframe instead of a wrapper div.

    0 讨论(0)
  • 2020-12-05 10:23

    In my case SSL certificate was invalid for iframe domain, so make sure that iframe URL you're trying to send messages to is opening w/o any issues (in case you load your iframe over https).

    0 讨论(0)
  • 2020-12-05 10:27

    Another reason this could be happening is if you are using an iframe that has the sandbox attribute and allow-same-origin isn't set e.g.:

    // page.html
    <iframe id="f" src="http://localhost:8000/iframe.html" sandbox="allow-scripts"></iframe>
    <script type="text/javascript">
        var f = document.getElementById("f").contentWindow;
        // will throw exception
        f.postMessage("hello world!", 'http://localhost:8000');
    </script>
    
    // iframe.html
    <script type="text/javascript">
        window.addEventListener("message", function(event) {
            console.log(event);
        }, false);
    </script>
    

    I haven't found a solution other than:

    • add allow-same-origin to the sandbox (didn't want to do that)
    • use f.postMessage("hello world!", '*');
    0 讨论(0)
  • 2020-12-05 10:33

    Make sure the target window that you (or Facebook) is posting a message to, has completed loading. Most of the times I've gotten this error were when an iframe I was sending messages to had failed to load.

    0 讨论(0)
  • 2020-12-05 10:34

    In my case I didn't add the http:// prefix. Potentially worth checking.

    0 讨论(0)
  • 2020-12-05 10:37

    To check whether the frame have been loaded, use onload function. Or put your main function in load: I recommend to use load when creating the iframe by js

     $('<iframe />', {
       src: url,
       id:  'receiver',
       frameborder: 1,
       load:function(){
         //put your code here, so that those code can be make sure to be run after the frame loaded
       }
       }).appendTo('body');
    
    0 讨论(0)
提交回复
热议问题