【问题标题】:Unsafe JavaScript attempt to access frame with URL (same domain!)不安全的 JavaScript 尝试使用 URL 访问框架(同一个域!)
【发布时间】:2012-05-09 22:49:23
【问题描述】:

从页面file://localhost/Users/pistacchio/dev/epress/catflow/test_html/index.html 我有以下(coffeescript)代码试图访问 iframe:

$('#ipad-viewport iframe').bind 'load', () ->
    console.log $(this).contents().find('map')

(这转换为以下 javascript,但我认为问题不在于这里):

(function() {

  $('#ipad-viewport iframe').bind('load', function() {
    return console.log($(this).contents().find('map'));
  });

}).call(this);

我等待加载 iframe 页面并尝试访问其正文中的元素。我收到以下错误:

Unsafe JavaScript attempt to access frame with URL file://localhost/Users/pistacchio/dev/epress/catflow/test_html/catalogo/catalog/intro.html from frame with URL file://localhost/Users/pistacchio/dev/epress/catflow/test_html/index.html. Domains, protocols and ports must match.

现在,因为 iframe 是这样定义的:

<iframe src="file://localhost/Users/pistacchio/dev/epress/catflow/test_html/catalogo/catalog/intro.html" width="1024" height="768"></iframe>

我的页面和 iframe 不是在同一个域中,还是 file://localhost?为什么我会遇到这个问题?

哦,如果相关的话,我正在使用 Chrome 18 进行测试。

【问题讨论】:

  • 似乎 file:// 并不是测试 JS/AJAX 的最佳包装器/protocoll。
  • 这不是一个 ajax 请求,我没有在服务器上运行,因为到目前为止还没有服务器端的工作要做。
  • 总是在服务器上运行它,这是测试它是否有效的最佳方法
  • 去掉返回,不要返回事件内部的东西,也许这会有所帮助。

标签: javascript jquery security


【解决方案1】:

file:/// URL 的 javascript 安全策略与适用于托管内容的普通 same origin policy 略有不同。为了阻止保存的网页读取磁盘的全部内容,不同的文件被视为不同的来源。只需启动本地服务器并在其上托管您的内容;您将退回到由域/ip 定义来源的“标准”策略。

如果由于某种原因您无法运行 Web 服务器,您可能会从 命令行开关:--allow-file-access-from-files。我相信这会影响将所有file:/// URL 定义为属于同一来源。

【讨论】:

  • 虽然这是我能找到的最接近明确的答案,但我不确定“--allow-file-access-from-files”开关有什么相关性——它是一个命令线路开关... 一个特定的浏览器?爪哇?
  • 命令行参数用于 Chrome,以回应 OP 说他使用的是 Chrome 18
猜你喜欢
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2013-02-02
  • 2011-05-18
相关资源
最近更新 更多