是否可以使用数据src inner document.domain设置iframe?

时间:2016-11-28 22:32:38

标签: javascript ajax angular iframe

有一个带有纯HTML src的iframe

<iframe id="myIframe"></iframe>

<script>
$.ajax({
    url: url,
    type: 'GET',
    success: function(data) {
        $('#myIframe').attr('src', 'data:text/html;charset=utf-8,' + data);
    }
});
</script>

如果要检查iframe的document.domain,那么它的值为&#39;&#39;或null。

我想将document.domain放到iframe。

为什么我要放置document.domain?作为响应HTML数据,有angular2应用程序,它使用SystemJS导入配置。它检查原点并发现它的iframe原点不同。结果 - 例外。

如果我错了,请纠正我。

谢谢!

*我使用ajax设置iframe src的原因是我需要在请求iframe src url中设置自定义标头。

1 个答案:

答案 0 :(得分:0)

这是你想要的隧道:

  1. 满足同源政策:
    1. 相同的域名(即example.comdomain.net等...)
  2. 使用子域隧道,绕过几个同源策略:
    1. 协议(即http://https://
    2. 子域名(即sub.domain.comapp.domain.comdocs.domain.com等......)
    3. 端口(即example.com:80example.com:8080example.com:21
  3. 两个页面上都必须出现代码段。
  4. &#13;
    &#13;
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no">
      <title>Tunnel</title>
      <style>
      </style>
    </head>
    
    <body>
    
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
      <script>
        /**
         * Replace $.ajax on your subdomain with a copy taken
         * from your base domain. All jQuery AJAX actions go
         * through $.ajax (i.e. $.get, $.post), so it's all good.
         */ // http://benv.ca/2011/03/07/subdomain-tunneling-with-jquery-and-document-domain/
        (function() {
          var iframe,
            onload,
            queue = [],
    
            // This has to be set both here and in iframe.html
            document.domain = 'example.com';
    
          // Back up this page's copy of $.ajax
          window.$._ajax = window.$.ajax;
    
          // We'll first replace $.ajax with a thin wrapper that both
          // loads our iframe tunnel and saves invocations until the
          // iframe is ready
    
          $.ajax = function(params) {
    
            if (!iframe) {
    
              // tunnel.html should be a bare bones html page that
              // includes a copy of jQuery, and sets document.domain
              // to 'example.com'
    
              iframe = $('<iframe>')
                .attr('src', 'http://example.com/tunnel.html')
                .load(onload)
                .appendTo('head')[0];
            }
    
            // Save calls to $.ajax, execute when we're ready
            queue.push(params);
          };
    
          function onload() {
    
            // Our prize: a version of $.ajax that can communicate safely
            // with our base domain
            window.$.ajax = iframe.contentWindow.jQuery.ajax;
    
            // Flush queued $.ajax calls
            $.each(queue, function(_, params) {
              $.ajax(params);
            });
    
            queue = null;
          }
        })();
      </script>
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;