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

标签 javascript jquery

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/

相关文章:

jquery 1.4.2 相当于 setTimeout 和clearTimeout

javascript - jquery 回发失败

javascript - GruntJs 'grunt' cmd 打开 Visual Studio?

javascript - 如何在javascript中显示访问对象的键

javascript - jQuery GetCity 未定义

javascript - 使用 Vue Router 在 Javascript 中将对象键设置为变量

javascript - Jquery 同时运行多个函数

javascript - 是否可以延迟 window.load?

javascript - jQuery draggable 在放下时移动

javascript - ESLint导入排序分组