Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML5跨域通信不起作用_Html_Cross Domain_Postmessage - Fatal编程技术网

HTML5跨域通信不起作用

HTML5跨域通信不起作用,html,cross-domain,postmessage,Html,Cross Domain,Postmessage,我打开1.htm作为一个http://127.0.0.1/1.html 1.html <!DOCTYPE html> <html> <head> </head> <body> <iframe id="ifr" src="http://localhost/2.html" width="100%" height="300"> </iframe>

我打开1.htm作为一个http://127.0.0.1/1.html

1.html

<!DOCTYPE html>
<html>
<head>

    </head>
    <body>
       <iframe   id="ifr" src="http://localhost/2.html" width="100%" height="300">
       </iframe>       
        <script>  
            iframe=document.getElementById("ifr");
            iframe.contentWindow.postMessage("hello there", "http://localhost");
     </script>  
    </body> 
    </html>
这是一个简单的例子。最后,我需要这样的结构:
在域A上,我有iframe,它的src是域B的页面。在iframe中,有一个按钮。当我点击显示在iframe中的那个按钮时,我需要调用域A的window.addEventListener,我该怎么做

检查主机文件C:\Windows\System32\drivers\etc,确保没有将localhost映射为其他文件


注意:通过Javascript从子对象访问父框架的工作方式与此不同,因为这会造成跨站点脚本编写的安全问题。

检查主机文件C:\Windows\System32\drivers\etc确保没有本地主机映射为其他内容

注意:通过Javascript从子脚本访问父框架的工作方式与此不同,因为这会造成跨站点脚本的安全问题。

如前所述。您只有以下选项

相同或不同域场景之间的通信:

 +-------------------------+-----------+-------------+-------------+
 |                         | home.html | framed.html | helper.html |
 +-------------------------+-----------+-------------+-------------+
 | www.foo.com/home.html   |    N/A    |     YES     |     YES     |
 | www.bar.net/framed.html |    NO     |     N/A     |     YES     |
 | www.foo.com/helper.html |    YES    |     YES     |     N/A     |
 +-------------------------+-----------+-------------+-------------+
这纯粹是针对CSRF跨站点请求伪造攻击的浏览器限制。看看你的schenario,即使你在同一个领域工作。一个选项是遵循上面的层次结构示例,您可以在页面之间传递消息,甚至可以在相同或不同的域中使用父站点上的帮助器页面。然后,孩子可以通过此帮助器页面接收和发送消息。

如前所述。您只有以下选项

相同或不同域场景之间的通信:

 +-------------------------+-----------+-------------+-------------+
 |                         | home.html | framed.html | helper.html |
 +-------------------------+-----------+-------------+-------------+
 | www.foo.com/home.html   |    N/A    |     YES     |     YES     |
 | www.bar.net/framed.html |    NO     |     N/A     |     YES     |
 | www.foo.com/helper.html |    YES    |     YES     |     N/A     |
 +-------------------------+-----------+-------------+-------------+
这纯粹是针对CSRF跨站点请求伪造攻击的浏览器限制。看看你的schenario,即使你在同一个领域工作。一个选项是遵循上面的层次结构示例,您可以在页面之间传递消息,甚至可以在相同或不同的域中使用父站点上的帮助器页面。然后,孩子可以通过此帮助器页面接收和发送消息。

解决方法是:

<iframe id="frameId" src="http://b.net/2.html"  onload="sendCommand();">  No Frame!</iframe>

            <script  type="text/javascript"> 
                    function sendCommand() {
                    var receiver;
                    receiver = document.getElementById('frameId').contentWindow;
                    receiver.postMessage(receiver, 'http://b.net');
                    }
            </script>
解决办法是:

<iframe id="frameId" src="http://b.net/2.html"  onload="sendCommand();">  No Frame!</iframe>

            <script  type="text/javascript"> 
                    function sendCommand() {
                    var receiver;
                    receiver = document.getElementById('frameId').contentWindow;
                    receiver.postMessage(receiver, 'http://b.net');
                    }
            </script>