【问题标题】:$('iframe').content(): Cannot get CORS passed$('iframe').content(): 无法通过 CORS
【发布时间】: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');下一个();

标签: jquery cors


【解决方案1】:

cors 标头不适用于 iframe。您需要使用 postMessage 方法跨不同域传输数据。见Cross domain iframe issue

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 2012-09-02
    • 2019-11-26
    • 2014-12-24
    • 2013-09-27
    • 2011-12-13
    • 2020-12-31
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多