无法从'HTMLIFrameElement'

时间:2016-10-29 16:18:24

标签: jquery

我可以自动填写iframe中外部网站内的表单,但我想加载页面已经有事件点击提交表单。

我尝试如下,但返回错误:无法从'HTMLIFrameElement'中读取'contentDocument'属性

 <?php header('Access-Control-Allow-Origin: *');  ?>
    <script type="text/javascript" src="jquery-3.1.1.min.js"></script>

    <iframe src="http://example.com/contact/" id="InnerIframe" height="100%" width="100%"></iframe>

    <script>

    $(window).on('load', function() {
       $('#InnerIframe').contents().find('submit').trigger( "click" );
    });

    </script>

控制台日志

Exception: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://localhost" from accessing a cross-origin frame.
    at contents (http://localhost/site/jquery-3.1.1.min.js:3197:14)
    at Function.map (http://localhost/site/jquery-3.1.1.min.js:457:13)
    at jQuery.fn.init.jQuery.fn.(anonymous function) [as contents] (http://localhost/site/jquery-3.1.1.min.js:3201:24)
    at http://localhost/site/:8:22
    at dispatch (http://localhost/site/jquery-3.1.1.min.js:5201:27)
    at elemData.handle (http://localhost/site/jquery-3.1.1.min.js:5009:28)contents @ jquery-3.1.1.min.js:3197map @ jquery-3.1.1.min.js:457jQuery.fn.(anonymous function) @ jquery-3.1.1.min.js:3201(anonymous function) @ (index):8dispatch @ jquery-3.1.1.min.js:5201elemData.handle @ jquery-3.1.1.min.js:5009
util.js:208 Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keysPA.j @ util.js:208(anonymous function) @ js?v=3.exp&key&ver=4.6.1:127(anonymous function) @ js?v=3.exp&key&ver=4.6.1:46(anonymous function) @ js?v=3.exp&key&ver=4.6.1:43(anonymous function) @ js?v=3.exp&key&ver=4.6.1:46(anonymous function) @ js?v=3.exp&key&ver=4.6.1:99(anonymous function) @ js?v=3.exp&key&ver=4.6.1:43(anonymous function) @ js?v=3.exp&key&ver=4.6.1:99(anonymous function) @ js?v=3.exp&key&ver=4.6.1:43(anonymous function) @ js?v=3.exp&key&ver=4.6.1:99ic @ js?v=3.exp&key&ver=4.6.1:45hc.Sb @ js?v=3.exp&key&ver=4.6.1:99(anonymous function) @ util.js:1

1 个答案:

答案 0 :(得分:0)

这是所有浏览器使用的正常安全措施。您无法访问具有不同来源的元素或框架,这将是一个巨大的安全漏洞。因此,浏览器会阻止尝试执行此类操作的所有脚本。要在框架内执行任何操作,其内容必须具有相同的原点。