【问题标题】:Posting to iframe, but the iframe redirects the top window to action url instead of self?发布到 iframe,但 iframe 将顶部窗口重定向到操作 url 而不是自己?
【发布时间】:2012-04-04 08:53:57
【问题描述】:

我正在使用此处提到的表单帖子加载带有一些帖子数据的 iframe:

How do you post to an iframe?

一切正常。 但是,如果我的 iframe 网址中有重定向(通过标头或 javascript sn-p), 它不会重定向到 iframe 中的下一个 url,而是重定向父窗口。

例如:发布到 iframe 中:

<form action="do_stuff.aspx" method="post" target="my_iframe">
  <input type="submit" value="Do Stuff!" />
</form>

<!-- when the form is submitted, the server response will appear in this iframe -->
<iframe name="my_iframe" src="not_submitted_yet.php"></iframe>

not_submitted_yet.php:

<?php

// DO some stuff with post data 
// redirect to a success url
header("Location: THE_URL");

?>

问题是 THE_URL 没有在 iframe 本身中打开,而是在完整的浏览器窗口中打开,这是不受欢迎的行为。 我怎样才能解决这个问题 ? Firefox 和 chrome 中的行为是相同的。

【问题讨论】:

    标签: php javascript iframe


    【解决方案1】:

    我认为这里可能发生的情况是 THE_URL 是一些破坏框架的受保护页面。也许维基百科或其他。使用这样的代码:

    if (top.location != location) {
        top.location.href = document.location.href ;
      }
    

    【讨论】:

    • ,不,我重定向你的 URL 是我的。
    • 那么目标可能由于某种原因不起作用,可能页面中存在另一个同名对象。您可以尝试将名称修改为更独特的名称,并将 id="thename" 添加到 iframe。所以 iframe 是同时按名称和 id 命名的。
    【解决方案2】:

    这并不能直接回答您的问题,但既然您已经在使用 javascript,为什么不通过 AJAX 帖子提交表单呢?返回新 URL 的 jQuery 帖子可以工作:

    var submitData = $('#yourForm').serialize();
    $.post('/your/url', submitData, function(data) {
        // Probably put some validation on the value coming back in data.
        window.location.href = data;
    });
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用 php 函数 file_get_contents,而不是标头重定向。

      <?php
      $success_page = file_get_contents('http://www.example.com/');
      echo $success_page;
      ?> 
      

      【讨论】:

      • 这样做的问题是你不能在file_get_contents中使用用户的参数,否则人们会开始使用你的页面作为某种代理来进行攻击。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2018-07-31
      • 2014-02-16
      • 2013-12-09
      • 2023-04-02
      相关资源
      最近更新 更多