How do I reload a page without a POSTDATA warning

2019-01-06 11:59发布

问题:

I want to reload a page using:

window.location.reload(true); 

But I receive the POSTDATA warning because the refresh function want to resend previous POST form data. How can I refresh my page without this warning?

UPDATED: I have no control of the project! I can't workaround the POST itself!

回答1:

Just changing window.location in JavaScript is dangerous because the user could still hit the back button and resubmit the post, which could have unexpected results (such as a duplicate purchase). PRG is a much better solution

Use the Post/Redirect/Get (PRG) pattern

To avoid this problem, many web applications use the PRG pattern — instead of returning an HTML page directly, the POST operation returns a redirection command (using the HTTP 303 response code (sometimes 302) together with the HTTP "Location" response header), instructing the browser to load a different page using an HTTP GET request. The result page can then safely be bookmarked or reloaded without unexpected side effects.



回答2:

You can't refresh without the warning; refresh instructs the browser to repeat the last action. It is up to the browser to choose whether to warn the user if repeating the last action involves resubmitting data.

You could re-navigate to the same page with a fresh session by doing:

window.location = window.location.href;


回答3:

I had some problems with anchor/hash-urls (including #) not reloading using the solution from Rex...

So I finally ended up by removing the hash part:

window.location = window.location.href.split("#")[0];


回答4:

To bypass POST warning you must reload page with full URL. Works fine.

window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname;


回答5:

how about window.location.replace(window.location.href);



回答6:

You can use JavaScript:

window.location.assign(document.URL);

Worked for me on Firefox and Chrome

check this link: http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get



回答7:

If you are at the stage where you are finished with the post data and simply want to view the page again afresh, you could just use a window.location and even maybe append a random string as a query paramater to guarantee a new version of the page.



回答8:

Nikl's version doesn't pass get query parameters, I used the following modified version:

window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname + window.location.search;

or in my case I needed to refresh the topmost page\frame, so I used the following version

window.top.location.href = window.top.location.protocol +'//'+ window.top.location.host + window.top.location.pathname + window.top.location.search;


回答9:

This worked

<button onclick="window.location.href=window.location.href; return false;">Continue</button>

The reason it didn't work without the

return false;
is that previously it treated that as a form submit button. With an explicit return false on it, it doesn't do the form submit and just does the reload of the same page that was a result of a previous POST to that page.



回答10:

<html:form name="Form" type="abc" action="abc.do" method="get" onsubmit="return false;">

method="get" - resolves the problem.

if method="post" then only warning comes.



回答11:

I've written a function that will reload the page without post submission and it will work with hashes, too.

I do this by adding / modifying a GET parameter in the URL called reload by updating its value with the current timestamp in ms.

var reload = function () {
    var regex = new RegExp("([?;&])reload[^&;]*[;&]?");
    var query = window.location.href.split('#')[0].replace(regex, "$1").replace(/&$/, '');
    window.location.href =
        (window.location.href.indexOf('?') < 0 ? "?" : query + (query.slice(-1) != "?" ? "&" : ""))
        + "reload=" + new Date().getTime() + window.location.hash;
};

Keep in mind, if you want to trigger this function in a href attribute, implement it this way: href="javascript:reload();void 0;" to make it work, successfully.

The downside of my solution is it will change the URL, so this "reload" is not a real reload, instead it's a load with a different query. Still, it could fit your needs like it does for me.



回答12:

using meta refresh in html

<meta http-equiv='refresh' content='1'>

using meta refresh in php

echo "<meta http-equiv='refresh' content='1'>"


回答13:

Here's a solution that should always work and doesn't remove the hash.

let currentPage = new URL(window.location.href);
currentPage.searchParams.set('r', (+new Date * Math.random()).toString(36).substring(0, 5));
window.location.href = currentPage.href;


回答14:

If you use GET method instead of POST then we can't the form filed values. If you use window.opener.location.href = window.opener.location.href; then we can fire the db and we can get the value but only thing is the JSP is not refreshing eventhough the scriplet having the form values.