window.location.replace - POST版本? [英] window.location.replace - POST version?
问题描述
window.location.replace()
可以向目标网址发出GET请求并替换浏览器返回历史记录。
window.location.replace()
can issue a GET request to target URL and replace the browser "back" history.
是否有任何方法可以向目标URL发出POST请求并替换浏览器返回历史记录(有/没有jQuery可以)?
Is there any method to issue a POST request to target URL and replace the browser "back" history as well (with/ without jQuery is fine)?
目前使用以下代码发出POST请求,但它不会取代返回历史记录:
Currently using the following code to issue a POST request, but it does not replace the "back" history:
var actionForm = $('<form>', {'action': 'register.php', 'method': 'post'}).append($('<input>', {'name': 'action', 'value': 'user_register', 'type': 'hidden'}));
actionForm.appendTo('body').submit();
推荐答案
你可能想做类似的事情。
you might want to do something like these.
$.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
}
}
});
我想也许你也可以摆脱使用这种方法删除历史记录的需要。
I'm thinking maybe you would also get rid of the need for removing the history using this approach.
或
您可以使用以下内容从历史记录中替换当前页面的记录并提交您的表格。
you can use the following to replace the record of the current page from the history and submit your form.
window.history.replaceState({}, 'foo', '/foo');
$("#form-id").submit();
或
您可以使用以下加载下一页并且您想从历史记录中删除下一页的记录时的代码:
you can use the following code when the next page loads and you want to remove the record of the next page from the history:
window.history.replaceState({}, 'foo', '/foo');
这篇关于window.location.replace - POST版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!