权限被拒绝同一域上的iframe

时间:2017-03-17 14:24:58

标签: javascript iframe dns

我在HTML页面中有一个iframe。 我的网站位于域名:abc.domain.local 我的iframe位于域名:def.domain.local

我做的一些测试甚至没有返回任何内容,然后一些返回Permission被拒绝访问属性'document'

我尝试了9种不同的方式(代码如下),但没有一种方法可行。 我在Firefox(最新版本)上安装了Cors Everywhere和禁用策略。

我已经安装了很多其他扩展程序来禁用Chrome和Firefox上的跨域策略,但没有任何结果。 我已经使用一些参数启动了chrome以禁用Web安全性,并且也没有工作

不幸的是我无法在def.domain.local上设置任何网页,而不是事件创建JS文件来使用postmessage

<html>
<head>
    <script src="js/jquery-3.1.1.js"></script>
</head>

<body>
    <input type="button" value="1" onclick="teste1()" />
    &nbsp;&nbsp;
    <input type="button" value="2" onclick="teste2()" />
    &nbsp;&nbsp;
    <input type="button" value="3" onclick="teste3()" />
    &nbsp;&nbsp;
    <input type="button" value="4" onclick="teste4()" />
    &nbsp;&nbsp;
    <input type="button" value="5" onclick="teste5()" />
    &nbsp;&nbsp;
    <input type="button" value="6" onclick="teste6()" />
    &nbsp;&nbsp;
    <input type="button" value="7" onclick="teste7()" />
    &nbsp;&nbsp;
    <input type="button" value="8" onclick="teste7()" />
    &nbsp;&nbsp;
    <input type="button" value="9" onclick="teste7()" />
    &nbsp;&nbsp;
    <input type="button" value="10" onclick="teste7()" />
    &nbsp;&nbsp;
    <br /><br />
    <iframe id="frame" name="frame" style="width: 1000px; height: 600px;" src="http://abc.domain.local">

    </iframe>

    <script type="text/javascript">
        document.domain = 'domain.local';

        function teste1(){
            console.log(document.getElementById('frame').contentWindow.document.body.innerHTML);
        }

        function teste2(){
            $.getJSON('http://def.domain.local/monitor.htm', function(data){
                console.log(data);
            });
        }

        function teste3(){
            console.log($("#frame").find("iframe").contents().find('body'));
            console.log($("#frame").find("iframe").contents().find('html'));

        }

        function teste4(){
            a = $("#frame").find("iframe").contents().find('body');
            console.log(a.innerHTML);
            console.log(a.html());
        }

        function teste5(){
            var iframeWindow1 = document.getElementById("frame").contentWindow;

            iframeWindow1.addEventListener("load", function() {
                var doc1 = iframe.contentDocument || iframe.contentWindow.document;
                var target1 = doc.getElementById("html");

                console.log(target1.innerHTML);
            });
        }

        function teste6(){
            $.ajax({
                url: 'http://def.domain.local/monitor.htm',
                type: 'GET',
                dataType: 'html',
                success: function (data) {

                    console.log(data);

                },
                error: function (data) {
                    console.log("error: " + data);
                    console.log(data);
                }
            });
        }

        function teste7(){
            $('iframe').contents().find('body').css('opacity','0.1');
        }

        function teste8(){
            var iframe = document.getElementById('frame');
            var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
            var iframeContent;

            iframeContent = iframeDocument.getElementById('body');

            var iframeWindow = iframe.contentWindow;

            // you can even call jQuery or other frameworks if it is loaded inside the iframe
            console.log(iframeWindow.jQuery('body'));

            // or
            console.log(iframeWindow.$('body'));

            // or even use any other global variable
            console.log(window.outside_iframe_variable);

        }

        function teste9(){
            var req = create("get", "http://def.domainlocal");
            if (req){
                req.onload = function (){
                    console.log(req.reposnseText)
                };
                req.send();
            }
        }

        function create(method, url){
            var xhr = new XMLHttpRequest();
            if ("withCredentials" in xhr){
                xhr.open(method, url, true);                    
            }
            else if (typeof XDomainRequest != "undefined"){
                xhr = new XDomainRequest();
                xhr.open(method, url);
            }
            else {
                xhr = null
            }
            return xhr;
        }

    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:-1)

我们终于实现了结果。 我们无法在FireFox上做,但在IE上非常简单。 只需通过自定义级别Internet选项启用访问跨域,然后通过ajax

Internet Explorer&gt;自定义级别&gt;启用访问数据源跨域

https://www.webdavsystem.com/ajax/programming/cross_origin_requests

$。AJAX({     网址:'http://def.domain.local',     类型:'GET',     dataType:'html',     成功:函数(数据){        的console.log(数据)     },     错误:function(data){         的console.log(数据);     } });