如何在不影响浏览器历史记录的情况下在iframe内提交表单?
我已经实现了跨域POST请求。它使用JavaScript创建并提交一个iframe内的表单。它能够工作,但是每个请求都会添加一项到浏览器的历史记录中。
有人知道解决此问题的方法吗?我已经尝试使用innerHTML和createElement创建iframe,但是没有看到任何区别。
PS- 我很想使用XMLHttpRequest(“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>