【问题标题】:Allow users to embed my content into their site - rails 4 (follow-up)允许用户将我的内容嵌入他们的网站 - rails 4(后续)
【发布时间】:2017-08-30 20:04:30
【问题描述】:

我希望允许用户将我的应用中的内容嵌入到他们的网站中。为此,我尝试运行以下解决方案:Allow users to embed my content into their sites (like blogs) -- rails 4

不幸的是,我遇到了 embed.js 中 iframe.src 的问题,因为它试图访问用户文件位置上的“嵌入”文件夹:

GET file:///.../embed/1 net::ERR_FILE_NOT_FOUND

#public/embed.js
window.onload = function() {

   //Params
   var scriptPram = document.getElementById('load_widget');
   var id = scriptPram.getAttribute('data-page');

   /iFrame
   var iframe = document.createElement('iframe');
   iframe.style.display = "none";
   iframe.src = "embed/" + id;
   document.body.appendChild(iframe);
};

如果我将 iframe.src 更改为指向我的应用的绝对 URL,例如“http://localhost:3000/embed”,然后我遇到了另一个错误:

Refused to display 'http://localhost:3000/embed/1' in a frame because it set
 'X-Frame-Options' to 'SAMEORIGIN'.

对不起,如果这个问题看起来很随机,我正在使用 ruby​​-on-rails 迈出第一步。我保证会改进:)

【问题讨论】:

    标签: ruby-on-rails iframe embed


    【解决方案1】:

    你需要修改你的控制器代码,我希望它能工作 -

    class Embed::PagesController < ApplicationController
     layout false
     after_action :allow_iframe, only: :show
    
     def show
        @page = Page.find params[:id]
     end
    
     private
    
     def allow_iframe
       response.headers.except! 'X-Frame-Options'
     end
    end
    

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2010-11-01
      • 1970-01-01
      • 2011-08-25
      • 2014-12-11
      • 2013-11-29
      相关资源
      最近更新 更多