【问题标题】:Embedding Google Apps Script in an iFrame在 iFrame 中嵌入 Google Apps 脚本
【发布时间】:2017-04-12 01:50:06
【问题描述】:

我正在尝试将使用 Google Apps 脚本中的 Javascript 动态构建的页面嵌入到 iFrame 中的我的网站中,但未显示 iFrame 的内容。 Google Apps 脚本具有阻止其加载的同源策略。

我想要做的是(我删除了完整的链接):

<iframe src="https://script.google.com/a/macros/SCRIPT_ID"></iframe>

我得到的错误是:

Refused to display 'https://script.google.com/a/macros/SCRIPT_ID' 
in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

有没有办法更改策略并将内容加载到 iFrame 中?

【问题讨论】:

    标签: javascript google-apps-script iframe web-applications same-origin-policy


    【解决方案1】:

    Google 最近刚刚启用了此功能。很长一段时间以来,它一直处于“功能请求”状态。 Link here

    您现在可以显式定义 X-Frame-Options。

    要允许嵌入另一个域,选项应该是 HtmlService.XFrameOptionsMode.ALLOWALL

    关于该主题的 Google 文档:

    https://developers.google.com/apps-script/reference/html/html-output#setXFrameOptionsMode(XFrameOptionsMode)

    例子:

    function doGet() {
        return HtmlService.createTemplateFromFile('form.html')
            .evaluate() // evaluate MUST come before setting the Sandbox mode
            .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
    }
    

    希望这会有所帮助!

    【讨论】:

    • 这仍然有效吗?因为我仍然得到X-Frame-Options' to 'SAMEORIGIN'.
    • @inrsaurabh 你找到解决方案了吗?即使在实施答案后我仍然遇到这个问题
    【解决方案2】:

    我在 doPost 响应表单上遇到了这个问题,仅在开发模式下,并通过在初始 doGet 表单上将 target="_self" 更改为 target="_top" 来纠正它。

       <form method="POST" id="ss-form" target="_top" action="<?!=SETTINGS.PUBLISHED_URL?>">
       

    【讨论】:

    • 希望我能再把它撞上一千倍。谢谢!
    • 你救我!谢谢!
    猜你喜欢
    • 2021-05-29
    • 2020-11-19
    • 1970-01-01
    • 2020-11-11
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多