【发布时间】:2014-09-11 18:41:59
【问题描述】:
我在http://mysite.dev:3000 上有一个iframe,此页面上的脚本会将iframe src 属性设置为http://mysite.dev:5000/somepage.html。
加载 iframe 后,我想使用此脚本访问其内容(在 http://mysite.dev:3000 上):
$('iframe').load(function() {
$('iframe').contents();
});
我总是得到错误:
Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin.
尽管如此,http://mysite.dev:5000/somepage.html 的响应标头遵循 CORS 准则:
Accept-Ranges:bytes
access-control-allow-credentials:true
access-control-allow-headers:Authorization, X-Requested-With, Content-Type, Origin, Accept
access-control-allow-methods:GET,PUT,POST,DELETE,OPTIONS
access-control-allow-origin:*
Cache-Control:public, max-age=0
我阅读了几个关于 SO 的问题并关注了它们,但仍然没有用。
【问题讨论】:
-
您从哪里获得这些标头值?数据包上的Wireshark?看到发送响应的实际代码可能会很好。
-
在服务器上设置:res.header('Access-Control-Allow-Origin', config.allowedDomains); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type');下一个();