【发布时间】:2016-09-24 18:24:17
【问题描述】:
我一直在努力更新一些旧版软件。我们最近更新了我们系统中使用的几个早就应该更新的工具,随着这些更新,我们的 jQuery 和 AngularJS 工作库发生了变化。
我已经让我们的大部分代码与新的更新一起工作,但这一段已经让我困扰了很长一段时间。有人告诉我,这在更新之前效果很好,之后就停止了。我无法测试这种说法。
使用ng-import,我们引入如下代码:
<iframe id="uploadFrame" width="250" height="50" frameborder="0" ng-src="/store/client/productscripts/binderCreate/partials/upload.html?dealer={{dealer}}" name="uploadFrame" scrolling="no" marginwidth="0" marginheight="0">
</iframe>
相关文件如下所示:
<html><head>(snip)</head><body>
<form id="uploadForm" enctype="multipart/form-data" method="post" action="http://other.ourdomain.com/clientbinders/upload?dealer={{dealer}}">
<input id="uploadFileInput" type="file" name="uploadFile">
</form>
</body></html>
用户使用 iFrame 中的文件输入来上传他们的新图像。然后,一旦它被加载,我们在 iFrame 之外有一个按钮,用于触发 iFrame 的表单提交。这样,我们可以让用户上传他们的图片并立即看到结果,而无需重新加载整个表单。在添加了大量跟踪代码以尝试确定 javascript 所看到的内容,并尝试了几种不同的访问 iFrame DOM 的方法(.contentWindow.document、.contentDocument 等)之后,此代码如下所示:
function uploadFile(){
var myIfrm = jQuery('#uploadFrame');
console.log("TEST #uploadFrame");
console.log(myIfrm);
console.log(myIfrm.length);
console.log("TEST #uploadFileInput");
console.log(myIfrm.contents().find('#uploadFileInput')); // ERROR HAPPENS HERE
console.log(myIfrm.contents().find('#uploadFileInput').length);
(snip: lots more tracking code and eventually a .submit() )
}
我在指定行收到的错误是:
错误:访问属性“文档”的权限被拒绝
由于 iframe 引用的是同一域上的页面,我不明白为什么应该拒绝该权限。当然,问题是:为什么我访问该 DOM 的权限被拒绝?或者,我应该如何做才能让它发挥作用?
【问题讨论】:
-
是的,这是一种已知行为。
-
使用 iframe 时请记住
http://www.example.com != http://example.com -
@PraveenKumar - 好的,那么已知的解决方案是什么?
-
@GeorgeLee - 是的,但我没有使用 www。一点也不。而且 iframe 没有引用域,它以斜杠开头并从那里继续 - 默认情况下,相同的域。
-
@PraveenKumar 搞定了。我不明白为什么,但我得到了它的工作。
标签: javascript jquery angularjs dom iframe