gpt4 book ai didi

javascript - window.location.replace - POST 版本?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:42 27 4
gpt4 key购买 nike

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');

关于javascript - window.location.replace - POST 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31911053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com