window.location.replace - POST版本?

时间:2015-08-10 02:58:53

标签: javascript jquery

window.location.replace()可以向目标网址发出GET请求并替换浏览器"返回"历史。

是否有任何方法可以向目标网址发出POST请求并替换浏览器"返回"历史也是如此(有/没有jQuery很好)?

目前使用以下代码发出POST请求,但它不会替换"返回"病史:

var actionForm = $('<form>', {'action': 'register.php', 'method': 'post'}).append($('<input>', {'name': 'action', 'value': 'user_register', 'type': 'hidden'}));
actionForm.appendTo('body').submit();

1 个答案:

答案 0 :(得分:2)

你可能想做类似的事情。

 $.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
         }
     }
 });

我想也许你也可以摆脱使用这种方法去除历史的需要。

OR

您可以使用以下内容从历史记录中替换当前页面的记录并提交表单。

window.history.replaceState({}, 'foo', '/foo');
$("#form-id").submit();

OR

您可以在下一页加载时使用以下代码,并且您想从历史记录中删除下一页的记录:

    window.history.replaceState({}, 'foo', '/foo');