简体   繁体   English

访问iframe文档对象时出现“访问被拒绝”错误

[英]“Access is denied” error on accessing iframe document object

For posting AJAX forms in a form with many parameters, I am using a solution of creating an iframe , posting the form to it by POST, and then accessing the iframe 's content. 为了在包含许多参数的表单中发布AJAX表单,我使用的是创建iframe的解决方案,通过POST将表单发布到它,然后访问iframe的内容。 specifically, I am accessing the content like this: 具体来说,我正在访问这样的内容:

$("some_iframe_id").get(0).contentWindow.document

I tested it and it worked. 我测试了它,它工作。

On some of the pages, I started getting an "Access is denied" error. 在某些页面上,我开始收到“访问被拒绝”错误。 As far as I know, this shouldn't happen if the iframe is served from the same domain. 据我所知,如果iframe是从同一个域提供的,那么这不应该发生。

I'm pretty sure it was working before. 我很确定它之前有效。 Anybody have a clue? 有人有线索吗?

If I'm not being clear enough: I'm posting to the same domain . 如果我不够清楚:我发布到同一个域名 So this is not a cross-domain request. 所以这不是跨域请求。 I am testing on IE only. 我只在IE上测试。

PS I can't use simple ajax POST queries (don't ask...) PS我不能使用简单的ajax POST查询(不要问......)

Solved it by myself! 自己解决了!

The problem was, that even though the correct response was being sent (verified with Fiddler), it was being sent with an HTTP 500 error code (instead of 200). 问题是,即使正在发送正确的响应(通过Fiddler验证),它也会被发送一个HTTP 500错误代码(而不是200)。

So it turns out, that if a response is sent with an error code, IE replaces the content of the iframe with an error message loaded from the disk ( res://ieframe.dll/http_500.htm ), and that causes the cross-domain access denied error. 事实证明,如果发送带有错误代码的响应,IE将使用从磁盘加载的错误消息替换iframe的内容( res://ieframe.dll/http_500.htm ),这会导致交叉-domain访问被拒绝错误。

Beware of security limitations associated to iFrames , like Cross domain restriction (aka CORS). 请注意与iFrame相关的安全限制,例如跨域限制(又称CORS)。 Below are 3 common errors related to CORS : 以下是与CORS相关的3个常见错误:

  1. Load an iFrame with a different domain. 加载具有不同域的iFrame。 (Ex: opening " www.foo.com " while top frame is " www.ooof.com ") (例如:打开“ www.foo.com ”,而顶部框架是“ www.ooof.com ”)

  2. Load an iFrame with a different port: iFrame's URL port differs from the one of the top frame. 使用不同的端口加载iFrame:iFrame的URL 端口与顶部框架的端口不同。

  3. Different protocols : loading iFrame resource via HTTPS while parent Frame uses HTTP. 不同协议:通过HTTPS加载iFrame资源,而父帧使用HTTP。

My issue was the X-Frame-Options HTTP header. 我的问题是X-Frame-Options HTTP标头。 My Apache configuration has it set to: 我的Apache配置将其设置为:

Header always append X-Frame-Options DENY

Removing it allowed it to work. 删除它允许它工作。 Specifically in my case I was using iframe transport for jQuery with the jQuery file upload plugin to upload files in IE 9 and IE 10. 特别是在我的情况下,我使用iframe传输jQuery与jQuery文件上传插件上传IE 9和IE 10中的文件。

Note if you have a iframe with src='javascript:void(0)' then javascript like frame.document.location =... will fail with Access Denied error in IE. 请注意,如果你有一个带有src='javascript:void(0)'的iframe,那么javascript就像frame.document.location =...将失败并且IE中的Access Denied错误。 Was using a javascript library that interacts with a target frame. 使用的是与目标框架交互的JavaScript库。 Even though the location it was trying to change the frame to was on the same domain as parent, the iframe was initially set to javascript:void which triggered the cross domain access denied error. 即使它尝试将帧更改为与父级在同一域中的位置,iframe最初也设置为javascript:void,这会触发跨域访问被拒绝错误。

To solve this I created a blank.html page in my site and if I need to declare an iframe in advance that will initially be blank until changed via javascript, then I point it to the blank page so that src='/content/blank.html' is in the same domain. 为了解决这个问题,我在我的网站中创建了一个blank.html页面,如果我需要提前声明一个iframe,最初将是空白的,直到通过javascript更改,然后我将其指向空白页面,以便src='/content/blank.html'在同一个域中。

Alternatively you could create the iframe completely through javascript so that you can set the src when it is created, but in my case I was using a library which reqired an iframe already be declared on the page. 或者你可以通过javascript完全创建iframe,这样你就可以在创建时设置src,但在我的情况下我使用了一个库,它需要在页面上声明一个iframe。

I know this question is super-old, but I wanted to mention that the above answer worked for me: setting the document.domain to be the same on each of the pages-- the parent page and the iframe page. 我知道这个问题已经过时了,但我想提一下上面的答案对我有用:在每个页面上设置document.domain是相同的 - 父页面和iframe页面。 However in my search, I did find this interesting article: 但是在我的搜索中,我找到了这篇有趣的文章:

http://softwareas.com/cross-domain-communication-with-iframes http://softwareas.com/cross-domain-communication-with-iframes

Basically, this error occurs when the document in frame and outside of ii have different domains. 基本上,当框架内和ii之外的文档具有不同的域时,会发生此错误。 So to prevent cross-side scripting browsers disable such execution. 因此,要防止跨端脚本浏览器禁用此类执行。

if it is a domain issue (or subdomain) such as www.foo.com sending a request to www.api.foo.com 如果是域名问题(或子域名),例如www.foo.com向www.api.foo.com发送请求

on each page you can set the 在每个页面上,您可以设置

document.domain = www.foo.com

to allow for "cross-domain" permissions 允许“跨域”权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “访问被拒绝”尝试访问以编程方式创建的<iframe>的文档对象时出现JavaScript错误(仅限IE) - “Access is denied” JavaScript error when trying to access the document object of a programmatically-created <iframe> (IE-only) 访问IFrame时IE9“访问被拒绝”错误 - IE9 “Access is Denied” Error, When Accessing IFrame 访问 IFrame 的 contentWindow.document 在 IE6 上给出“访问被拒绝” - Accessing IFrame's contentWindow.document gives 'Access is Denied' on IE6 SecurityError:当访问 iFrame 的信息时,访问跨域 object 上的属性“文档”的权限被拒绝 - SecurityError: Permission denied to access property “document” on cross-origin object when acces info of a iFrame iframe contentWindow在缩短document.domain后抛出Access Denied错误 - iframe contentWindow throws Access Denied error after shortening document.domain 从iframe内部访问属性时,访问被拒绝 - Access denied when accessing properties from inside an iframe Edge&IE:创建iframe并访问窗口对象后,权限被拒绝 - Edge & IE: Permission Denied after creating an iframe and accessing window object 如何从包含文档对象访问iframe - How to access an iframe from the including document object 错误:拒绝访问属性“文档”的权限 - Error: Permission denied to access property “document” 错误:拒绝访问属性“文档”的权限 - Error: Permission denied to access property 'document'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM