【发布时间】:2016-03-08 06:25:38
【问题描述】:
我知道 iframe 标记可以访问具有相同域 + 端口的父元素。 但是,如果 parent 和 iframe 具有不同的域 + 端口怎么办?
即
parent 的域是http://aaa.com:63342,iframe 的域是http://aaa.com:9080。(请注意它们有不同的端口)
两个页面的标题中都有<meta http-equiv='X-Frame-Options' content='ALLOWAll'>。
- 首先,父框架调用 iframe 并提交表单。喜欢...
<!DOCTYPE html> <html> <head> <meta http-equiv='X-Frame-Options' content='ALLOWAll'> <title>ParentWindows with aaa.com:63342</title> </head> <body> <form name='form' method='post' id='form' action=''> <input type='text' name='greetings' value='Hello from the otherside'> </form> <script> document.form.target = 'iframe'; document.form.action = 'http://aaa.com:9080//inFrame.jsp'; document.form.submit(); </script> </body> <iframe src="" name='iframe'></iframe> </html>
- 然后一个服务器在jsp中返回如下
<% response.setHeader("X-Frame-Options", "ALLOWAll"); String greetings = request.getParameter("greetings"); %> <!DOCTYPE html> <html> <head> <meta http-equiv='X-Frame-Options' content='ALLOWAll'> <title>iframe with aaa.com:9080</title> </head> <body> <div> greetings message : <%= greetings %> </div> </body> <script> var div = document.createElement('div'); div.textContent = 'Echo Hello'; parent.document.body.appendChild(div); </script> </html>
这是我所处情况的简单版本。但是,当我这样做时,浏览器控制台会显示类似错误..
Uncaught SecurityError: Blocked a frame with origin "http://localhost:9080" from accessing a frame with origin "http://localhost:63342". Protocols, domains, and ports must match.
现在我首先怀疑这种方法(在 iframe 和父级之间调用不同的主机)是否可能......这可能吗?
我怎样才能使它工作?
非常感谢
【问题讨论】:
标签: html jsp iframe popup x-frame-options