我想让用户将我的应用程序中的内容嵌入到他们的站点中。为此,我尝试运行以下解决方案:Allow users to embed my content into their sites (like blogs) -- rails 4
不幸的是,我遇到了iframe.src在embed.js中遇到的问题,因为它试图访问用户文件位置上的“嵌入”文件夹:
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'.对不起,如果这个问题看起来很随意的话,我已经开始用红宝石了。我承诺改善:)
发布于 2017-09-11 16:36:48
你需要修改你的控制器代码,我希望它能工作-
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
endhttps://stackoverflow.com/questions/43227267
复制相似问题