【问题标题】:Load jQuery in iframe and use ajax of it在 iframe 中加载 jQuery 并使用它的 ajax
【发布时间】:2013-03-07 16:10:11
【问题描述】:

我已经为跨域请求创建了带有 iframe 的 hta 应用程序,因为我无法启动自己的服务器来代理这些请求。

在主页上,我插入了“受害者”网站的 jQuery 脚本和 iframe,例如:google.com

<iframe name="iframe" src="http://google.com" application="yes"></iframe>

现在我必须通过 iframe 执行 AJAX POST 请求,代码如下:

var frame = document.frames.iframe;
frame.$ = $
frame.$.ajax({
    type : 'post'
    url : 'http://google.com'
    data : 'blablabla'
});

但是 iframe 给了我“无传输”错误。但是当我在受害者页面上做脚本元素时,一切正常,例如:

var frame = document.frames.iframe.document;
var el = frame.createEelement('SCRIPT');
el.src = "file://urlToJs.js";
frame.appendChild(el);

这个例子一切正常,但我没有工作互联网(我们使用本地站点)......并创建另一个 js 文件来加载我也不想要。

请帮帮我!

我尝试过的:

  • 获取脚本元素文本 (document.scripts[0].innerHTML) 并创建 iframe 中带有文本的脚本 [错误]
  • document.frames.iframes.$ = parent.$ [也不起作用,没有传输错误]

似乎 jQuery ajax 总是使用主页来创建请求,我该如何解决?

【问题讨论】:

  • 如果 iframe 属于不同的域,则无法对其执行任何操作。
  • 在 hta 中具有属性:application="yes" 也可以跨域访问。找到了答案,答案是 $.support.cors = true 并且在 ajax crossDomain 中:true。

标签: javascript jquery ajax post iframe


【解决方案1】:

jquery 收集脚本加载所需的一切。

你可以在jquery源文件中看到这样的东西:

(function factory( window ){ // a huge closure around the entire file
    var doc = window.document;

    window.$ = function(){
        // do something with the "doc"
    }
})( window );  // <- init with current window

所以无论你在哪里分配变量 $,“文档”都不会改变。

jquery 这样做是为了解决性能问题(也许是其他的?

你需要编辑jquery源,提取“工厂”函数...

( 函数工厂(){} )() ==extract==> 函数工厂(){}

并用 iframe 的窗口调用它

工厂(iframe.contentWindow);

那么你可以随意使用 iframe.contentWindow.$。

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多