是否可以在 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>