有一个带有纯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中设置自定义标头。
答案 0 :(得分:0)
这是你想要的隧道:
example.com
,domain.net
等...)http://
或https://
)sub.domain.com
,app.domain.com
,docs.domain.com
等......)example.com:80
,example.com:8080
,example.com:21
)
<!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;