admin 管理员组文章数量: 1086019
window.location.replace()
can issue a GET request to target URL and replace the browser "back" history.
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)?
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();
window.location.replace()
can issue a GET request to target URL and replace the browser "back" history.
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)?
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();
Share
Improve this question
asked Aug 10, 2015 at 2:58
RaptorRaptor
54.3k47 gold badges248 silver badges399 bronze badges
1
- 1 please check my answer below, it should work if you are open to use AJAX request. – Allan Chua Commented Aug 10, 2015 at 3:16
1 Answer
Reset to default 3you 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.
OR
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();
OR
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');
本文标签: javascriptwindowlocationreplacePOST versionStack Overflow
版权声明:本文标题:javascript - window.location.replace - POST version? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744093501a2532471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论