【问题标题】:Can I use javascript/jquery to submit a non-local form in an iframe?我可以使用 javascript/jquery 在 iframe 中提交非本地表单吗?
【发布时间】:2011-04-06 21:06:07
【问题描述】:

我正在http://www.localurl.com 建立一个页面。

我想将 http://www.url.com/form_step_1.html 上的 POST 表单提交到 http://www.url.com/form_step_2.html 并在 iframe 中显示 http://www.url.com/form_step_2.html。

这可能与 JavaScript/jQuery 相关还是浏览器安全性禁止这样做?无论如何,它可以用ajax完成吗?

除了选定的答案之外,我发现this blog post 提供了有关该主题的非常丰富的信息。

【问题讨论】:

    标签: javascript ajax iframe jquery


    【解决方案1】:

    您无法在您无权访问的其他域上执行 Javascript。

    如果您有访问权限,请使用:

    document.domain = 'example.com'
    

    在两个页面上。然后,您可以在 iFrame 中提交一个表单:

    window.top.myiframename.document.myformname.submit();
    

    或

    window.myiframename.document.myformname.submit();
    

    注意:这可能不适用于 IE8。

    或者,您可以使用 php 在您的域上创建自己的页面:

    <?php
        echo file_get_contents("http://www.url.com");
    ?>
    

    在您的 iframe 中打开此页面(位于您的本地主机上)并使用:

        window.top.myiframename.document.myformname.submit();
    

    或 window.myiframename.document.myformname.submit();

    【讨论】:

      【解决方案2】:

      这不是您想要的,但您可以提交一个表单,并将其 target 设置为 iframe 的名称。

      在foo.com:

      <form action="http://bar.com/action" method="POST" target="baz">
        ...
      </form>
      
      <iframe name="baz" ...></iframe>
      

      【讨论】:

        猜你喜欢
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-07
        • 2011-03-25
        • 2021-01-01
        • 1970-01-01
        相关资源
        最近更新 更多