【问题标题】:jQuery cross domain iframe scriptingjQuery 跨域 iframe 脚本
【发布时间】:2011-03-06 05:11:19
【问题描述】:

我正在尝试访问加载外部 URL 的 iframe 的 DOM。当然,由于跨域安全性,我收到“权限被拒绝”错误。我怎样才能使这项工作?我看到了一些用 json 完成的东西(但我无法从我的外部源获得一个 json 字符串)和一些用 HTML5 postmessage 完成的东西。

您可以在以下位置看到它: http://jsfiddle.net/QPBvJ/

代码是:

    $(document).ready(function(){
     $('#get').live('click', function() {

        var currentIFrame = $('#frameDemo');
        currentIFrame.contents().find("a").css("background-color","#BADA55");

        alert ("done")
    });
    });

<iframe src="http://api.jquery.com/" width="80%" height="600" id='frameDemo'></iframe>
<button id="get">Get</button>

完成这项工作的最简单方法是什么。 谢谢

【问题讨论】:

标签: jquery cross-domain


【解决方案1】:

没有办法使这项工作。除非您尝试访问的外部域支持C.O.R.SJSONPpostMessage 之类的过程。

有一些例外(和往常一样):

例如,如果您正在处理一个 WebApp,您可以告诉您的用户他们必须授予对 cross-domain-calls 的访问权限。

例如在 Gecko/Firefox 中,你可以调用

netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead')

允许浏览器通过ajax/iframes 访问外部域。在这种情况下,用户必须设置

signed.applets.codebase_principal_support

发送至about:config 下的true 以完成这项工作。

在这个世界的Internet Explorers中,有一个类似allow cross-domain access的设置深深隐藏在security标签中,必须设置为enable

Chrome 允许使用命令行参数进行跨域调用:

chrome.exe --disable-web-security

【讨论】:

猜你喜欢
  • 2017-05-09
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多