簡體   English   中英

是否可以使用data src內部document.domain設置iframe?

[英]Is it possible to set iframe with data src inner document.domain?

有一個帶有純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,則其值為”或null。

我想將document.domain放入iframe。

為什么要放置document.domain? 為了響應HTML數據,提供了angular2應用,該應用使用SystemJS導入配置。 它檢查來源,並發現其iframe的來源不同。 結果-例外。

如果我錯了,請糾正我。

謝謝!

*之所以使用ajax設置iframe src,是因為我需要在請求中設置自定義標頭,以發送給iframe src url。

這是您想要的隧道:

  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. 該摘錄必須同時出現在兩個頁面上。

SNIPPET

 <!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 (ie $.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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM