如何使用 iframe 在此应用的另一个页面中显示 rails 个应用的页面?
How to display rails app's page inside another page of this app with iframe?
我必须在 iframe 内的这个应用程序的另一个页面上显示我的页面,这是我的应用程序的一部分
例如我有 /customers
页面和 /invoices 页面(两个操作都是 #index
)。 /invoices
页面有带 src='http://localhost:3000/customers'
的 iframe。
如何使其正常工作(也在生产中,意味着 example.com/invoices
必须在 iframe 中显示 example.com/customer
)?
我已经尝试过 Whosebug 的其他解决方案:
response.headers.delete('X-Frame-Options')
在 CustomerController#index
里面(还有 before_action
& after_action
)
- 设置
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }
- 在
ApplicationController
级别删除 headers,如 CustomerController
- 设置
response.headers['X-Frame-Options'] = 'ALLOWALL'
但没有任何效果。每次我在 Chrome 中得到 localhost refused to connect.
,在 Firefox 中得到 Firefox Can’t Open This Page
。
它也不适用于相对路径,例如iframe src='/customers'
我在 HTTP headers 中找到了解决方案。
签出 Content-Security-Policy header 并在 config/initializers/secure_headers.rb
中设置为 none
所以问题的解决方案是设置frame_ancestors: %w['self']
SecureHeaders::Configuration.default do |config|
config.csp = {
...
frame_ancestors: %w['self'], # Replaced from frame_ancestors: %w['none'],
...
}
end
并检查 HTTP headers,Content-Security-Policy
header 包含 frame-ancestors 'self';
它在开发中工作,例如在生产中工作
我必须在 iframe 内的这个应用程序的另一个页面上显示我的页面,这是我的应用程序的一部分
例如我有 /customers
页面和 /invoices 页面(两个操作都是 #index
)。 /invoices
页面有带 src='http://localhost:3000/customers'
的 iframe。
如何使其正常工作(也在生产中,意味着 example.com/invoices
必须在 iframe 中显示 example.com/customer
)?
我已经尝试过 Whosebug 的其他解决方案:
response.headers.delete('X-Frame-Options')
在CustomerController#index
里面(还有before_action
&after_action
)- 设置
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }
- 在
ApplicationController
级别删除 headers,如CustomerController
- 设置
response.headers['X-Frame-Options'] = 'ALLOWALL'
但没有任何效果。每次我在 Chrome 中得到 localhost refused to connect.
,在 Firefox 中得到 Firefox Can’t Open This Page
。
它也不适用于相对路径,例如iframe src='/customers'
我在 HTTP headers 中找到了解决方案。
签出 Content-Security-Policy header 并在 config/initializers/secure_headers.rb
none
所以问题的解决方案是设置frame_ancestors: %w['self']
SecureHeaders::Configuration.default do |config|
config.csp = {
...
frame_ancestors: %w['self'], # Replaced from frame_ancestors: %w['none'],
...
}
end
并检查 HTTP headers,Content-Security-Policy
header 包含 frame-ancestors 'self';
它在开发中工作,例如在生产中工作