简体   繁体   English

js跨域到iframe

[英]js cross-domain to iframe

I have the iframe from different domain imbedded in my page. 我的网页中嵌入了来自其他域的iframe。 Is it possible to have access to its DOM from a parent page with some way? 是否可以通过某种方式从父页面访问其DOM? Now it says: 现在它说:


Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin " http://localhost:63342 " from accessing a cross-origin frame. 未捕获的DOMException:无法从“ HTMLIFrameElement”读取“ contentDocument”属性:阻止了源为“ http:// localhost:63342 ”的框架访问跨域框架。


I hoped that adding the directive 我希望添加指令

<?php header('Access-Control-Allow-Origin: *');  ?>

to the iframe page will help, but it doesn't. 到iframe页面会有所帮助,但没有帮助。 Both domains now are located on my localhost but under different servers running. 这两个域现在都位于我的本地主机上,但在运行的不同服务器下。 So is there some working solution? 那么有什么可行的解决方案吗? Notice that I need just an access to some iframe's elements, it's not about postMessages, websockets etc. 请注意,我只需要访问某些iframe元素,而不是关于postMessages,websockets等。

Notice that I need just an access to some iframe's elements 请注意,我只需要访问某些iframe元素

Direct access is impossible 直接访问是不可能的

it's not about postMessages 这与postMessages无关

postMessage can achieve the same ultimate goal. postMessage可以实现相同的最终目标。

You just need to move the code which accesses the DOM to the page inside the iframe, and then use postMessage from the parent window to ask that page to run it. 您只需要将访问DOM的代码移动到iframe内的页面,然后使用父窗口中的postMessage要求该页面运行它。 That page can then serialise any data the parent window needs to a string and use postMessage to send it back. 然后,该页面可以将父窗口需要的任何数据序列化为字符串,并使用postMessage将其发送回去。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM