在iframe中形成POST而不影响历史记录

Bri*_*ian 9 javascript ajax cross-domain browser-history

是否可以在iframe中提交表单而不影响浏览器的历史记录?

我已经实现了发送跨域POST请求.它使用Javascript在iframe中创建和提交表单.它有效,但每个请求都会在浏览器的历史记录中添加一个项目.

有人知道解决这个问题吗?我尝试用innerHTML和createElement创建iframe.到目前为止,我没有看到任何差异.

PS - 我很想使用XMLHtttpRequest("Ajax"),但它不支持跨域发送数据.我希望使用GET而不是post,但我需要发送超过2k的数据.

这是我的代码的一个版本.我已经尝试了很多变化并且已经搜索过了,但我似乎无法找到一个不影响浏览器历史的解决方案.我相信这是不可能的 - 任何人都可以证实吗?

<html>

<head>
  <script type="text/javascript">
    function submit(params) {

      var div = document.createElement('div');
      div.innerHTML = '<iframe height="50" width="50"></iframe>';
      document.body.appendChild(div);

      var iframe = div.firstChild;
      var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
      iframeDocument.open();
      iframeDocument.close();

      var form = iframeDocument.createElement('form');
      iframeDocument.body.appendChild(form);
      form.setAttribute('action', 'http://some-other-domain.com/submit-here');
      form.setAttribute('method', 'POST');

      for (param in params) {
        var field = iframeDocument.createElement('input');
        field.setAttribute('type', 'hidden');
        field.setAttribute('name', param);
        field.setAttribute('value', params[param]);
        form.appendChild(field);
      }
      form.submit();
    }

    window.onload = function() {
      document.getElementById('button').onclick = function() {
        submit({
          'x' : 'Some Value',
          'y' : 'Another Value',
          'z' : new Date().getTime()
        });
      }
    }
  </script>
</head>

<body>
  <h1>Example of using Javascript to POST across domains...</h1>
  <input id="button" type="button" value="click to send">
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Ben*_*pan 1

使用 JS 添加一个 IFRAME 是否有效,该 IFRAME 的 src 托管在您的站点(第三方托管脚本需要向其发送数据的域?)此 IFRAME 可以包含所需的 Javascript,以便向您/其发出 XMLHttpRequest领域。至于从第三方站点获取实际数据到此 IFRAME - 尝试: http: //softwareas.com/cross-domain-communication-with-iframes。这是一个非常聪明的解决方案,涉及更改 IFRAME URL 末尾的片段标识符 (#something),然后您可以通过 IFRAME 中的 JS 读取该标识符。

也是一个猜测,但如果您将过去的解决方案添加到上面的类似历史问题(使用 location.replace),这希望应该让您在不破坏历史堆栈的情况下完成锚点更改部分。