window.location.replace()
可以向目标URL发出GET请求并替换浏览器的“后退”历史记录。
是否有任何方法可以发出针对目标URL的POST请求并同时替换浏览器的“后退”历史记录(使用/不使用jQuery都可以)?
当前使用以下代码来发出POST请求,但不能代替“返回”历史记录:
var actionForm = $('<form>', {'action': 'register.php', 'method': 'post'}).append($('<input>', {'name': 'action', 'value': 'user_register', 'type': 'hidden'}));
actionForm.appendTo('body').submit();
您可能想做这样的事情。
$.ajax({
url: "your url here",
type: "POST",
data: {
field1: "value1",
field2: "value2",
field3: "value3"
},
success: function (response) {
if (response.successFlag) {
//Replace current location from the history via history API
window.history.replaceState({}, 'foo', '/foo');
window.location = "url of target location here if you want to send a get request";
$("#form-id").submit();//if you want to post something up
}
}
});
我在想,也许您也将摆脱使用这种方法删除历史记录的需要。
要么
您可以使用以下内容从历史记录中替换当前页面的记录,然后提交表单。
window.history.replaceState({}, 'foo', '/foo');
$("#form-id").submit();
要么
您可以在加载下一页并要从历史记录中删除下一页的记录时使用以下代码:
window.history.replaceState({}, 'foo', '/foo');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句