我有一个带有多个WooCommerce安装的Wordpress多站点。我正在尝试通过iFrame获取整个安装过程中所有区域的仪表板报告:
等等
在主仪表板页面上,我使用以下代码注入所需的iFrame:
jQuery(document).ready(function($) {
/*If the user is on the main dashboard...*/
if(window.location.href.indexOf("https://foo.com/wp-admin/network/") > -1) {
console.log('Admin Dashboard - Activating global iFrame stats');
/*Create area for iFrames*/
$('#dashboard-widgets').after('<div class="iframe-container"><strong>Cross Network Stats (US, UK, AU, EU)</strong></div>');
/*United States*/
/*Add US iFrame (on same domain - working)*/
$('.iframe-container').append('<div><strong style="font-size: 2em;">United States:</strong><br><br><iframe src="https://foo.com/wp-admin/edit.php?post_type=shop_order" width="100%" height="450px"></iframe></div>');
/*United Kingdom*/
/*Add UK iFrame (on subdomain - not working)*/
$('.iframe-container').append('<div><strong style="font-size: 2em;">United Kingdom:</strong><br><br><iframe src="https://en.foo.com/wp-admin/edit.php?post_type=shop_order" width="100%" height="450px"></iframe></div>');
}
});
第一个iFrame按预期工作,但第二个没有,因为它在iFrame中并且会产生X-Frame安全性错误:
X-Frame-Options拒绝加载:https://en.foo.com/wp-admin/edit.php?post_type=shop_order不允许跨源框架。
我已经读过,可以通过使用'document.domain'来避免这种情况。因此,我在全局管理页脚中将以下代码注入我的页脚:
echo '<script>document.domain = "foo.com";</script>';
但是,我仍然遇到和以前一样的问题。
我是否需要在通过jQuery调用iFrames时触发document.domain作为javascript函数?
你必须改变你的Content-Security-Policy
标题。
Content-Security-Policy: frame-ancestors 'self' *.foo.com
This answer更深入一点。