SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement

依然范特西╮ 提交于 2019-12-14 04:07:42

问题


I have on a webpage an iframe element that loads my other website, and I'm trying to access the content of that webpage in an iframe. This works when both websites are on localhost, but on different domains I get this error : SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement Blocked a frame with origin "http://site1.com" from accessing a frame with origin "http://site2.com". Protocols, domains, and ports must match.

I have control over both websites, I can disable headers or add new. Is there a technical way to enable first site reading the contents of the iframe ?


回答1:


You need to set the Access-Control-Allow-Origin to allow cross-origin resources. This shouldn't be a problem, since you control both domains.

If you're using PHP (for example), you could set the header on site2 like:

header('Access-Control-Allow-Origin: site1.com');

You can use a similar function for other server software.



来源:https://stackoverflow.com/questions/26657687/securityerror-failed-to-read-the-contentdocument-property-from-htmliframeele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!