Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Javascript 访问控制允许源站和document.domain不工作_Javascript_Php_Iframe_Cross Domain_Access Control - Fatal编程技术网

Javascript 访问控制允许源站和document.domain不工作

Javascript 访问控制允许源站和document.domain不工作,javascript,php,iframe,cross-domain,access-control,Javascript,Php,Iframe,Cross Domain,Access Control,我在PHP页面中有一个iframe,我试图从iframe中访问父元素,但总是在控制台中出现以下错误: 未捕获的DomeException:用原点阻止帧 从访问交叉原点帧 我已经阅读了谷歌首页上关于同一原产地政策和访问控制允许原产地的每一条搜索结果,但这似乎并没有解决我的问题。我仍然得到同样的错误 以下是我迄今为止所做的工作: 在我的iframe内容中添加了访问控制允许来源 在我的iframe内容中添加了document.domain 还有其他人有类似的问题吗?我能做些什么来修复它 这是我的密码:

我在PHP页面中有一个iframe,我试图从iframe中访问父元素,但总是在控制台中出现以下错误:

未捕获的DomeException:用原点阻止帧 从访问交叉原点帧

我已经阅读了谷歌首页上关于
同一原产地政策
访问控制允许原产地
的每一条搜索结果,但这似乎并没有解决我的问题。我仍然得到同样的错误

以下是我迄今为止所做的工作:

  • 在我的iframe内容中添加了
    访问控制允许来源
  • 在我的iframe内容中添加了
    document.domain
  • 还有其他人有类似的问题吗?我能做些什么来修复它

    这是我的密码:

    <?php
    header('Access-Control-Allow-Origin: https://b.example.com');
    ?>
    
    <script>
        function receiveMessage(event) {
            if (event.origin !== "https://a.example.com")
                return;
        }
        window.addEventListener("message", receiveMessage, false);
    </script>
    
    
    <div>
        <button title="Close (Esc)" type="button" class="close">×</button>
    </div>
    
    <iframe name="myIframe" class="myIframe"
            src="https://b.example.com" width="100%" height="600px"
            frameborder="0" allowfullscreen="" webkitallowfullscreen="" mozallowfullscreen="">
    </iframe>
    
    </div>
    
    
    函数接收消息(事件){
    如果(event.origin!==”https://a.example.com")
    返回;
    }
    window.addEventListener(“消息”,receiveMessage,false);
    ×
    
    b.example.com中的脚本

    <script>
        window.parent.$('.close').click(function () {
            var thisObj = this;
            if (!feedbackLoaded) {
                $('#openFeedback').click();
                feedbackLoaded = true;
                $('#feedbackForm .close').click(function () {
                    window.parent.$.magnificPopup.proto.close.call(thisObj);
                });
                return false;
            } else {
                window.parent.$.magnificPopup.proto.close.call(thisObj);
            }
        });
    </script>
    
    
    window.parent.$('.close')。单击(函数(){
    var thisObj=此;
    如果(!反馈加载){
    $('#openFeedback')。单击();
    反馈加载=真;
    $('#feedbackForm.close')。单击(函数(){
    window.parent.$.magnificpoop.proto.close.call(thisObj);
    });
    返回false;
    }否则{
    window.parent.$.magnificpoop.proto.close.call(thisObj);
    }
    });
    
    无论如何,您都可以实现文档和iframe之间的通信

    在这种情况下,您必须指定父窗口上可用的所有操作。以下是一个例子:

    父窗口:

    <html>
    <head>
        <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    </head>
    <body>
    <script>
        function receiveMessage(event) {
            console.log(event);
            if (event.data.message === 'Hello world') {
                $('#openFeedback').click();
            }
        }
        window.addEventListener("message", receiveMessage, false);
    </script>
    
    
    <div>
        <button title="Close (Esc)" type="button" class="close">×</button>
    </div>
    
    <iframe name="myIframe" class="myIframe"
            src="http://b.test.dev" width="100%" height="600px"
            frameborder="0" allowfullscreen="" webkitallowfullscreen="" mozallowfullscreen="">
    </iframe>
    
    </body>
    </html>
    
    
    函数接收消息(事件){
    console.log(事件);
    如果(event.data.message==='Hello world'){
    $('#openFeedback')。单击();
    }
    }
    window.addEventListener(“消息”,receiveMessage,false);
    ×
    
    和iframe中的子域:

    <html>
    <head>
        <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    </head>
    <body>
    <script>
        window.parent.postMessage({message: 'Hello world'}, 'http://test.dev');
    </script>
    </body>
    </html>
    
    
    window.parent.postMessage({message:'helloworld'},'http://test.dev');
    

    此外,您还应该为iframe标识提供一些安全检查,就像您在示例中添加的那样。在这种情况下,您不需要添加跨原点标题

    从iframe处理父窗口始终是一种糟糕的做法。即使域是sameI,我也理解你,但当你在一个每天有数百万用户的十年应用程序上工作时,您知道大多数小的修改都是解决办法,而不是一个合适的解决方案:)就是这样,我们必须找到一个现有系统的解决方案OK:)然后您必须向父窗口添加指令(访问控制允许源代码)应该在那里。然后你可以通过postMessage与它通信:Hi@AlexSlipknot我正在尝试使用你链接中给出的示例,但对我来说它说:
    无法读取测试时未定义的属性'postMessage'。php:4
    在我的iframe上,我在父页面上定义。我想你错过了一些选择器。因为您有“未定义”的消息,奇怪的是,它在没有在父对象上添加任何事件侦听器和在iframe上添加postMessage的情况下对我有效。我所做的只是在父项的头部添加了jquery文件,它工作正常。很抱歉,我一直在同一个域上运行这两个文件。让我试一试,你成功了吗?:)您好,朋友,您提到“您必须指定父窗口上可用的所有操作”。不幸的是,我无法将应用程序逻辑移动到父窗口。它必须保持在iframe中。但正如您所看到的,iframe中的代码试图访问父页面的元素,我得到了跨域错误,明白了!我在iframe和parent上都使用了window.postMessage和eventListener,看起来一切正常。非常感谢:)