如何在 Javascript 中重新加载页面而不出现 POSTDATA 警告?

发布于 2024-07-13 21:18:59 字数 190 浏览 6 评论 0原文

我想使用以下命令重新加载页面:

window.location.reload(true); 

但我收到 POSTDATA 警告,因为刷新功能想要重新发送以前的 POST 表单数据。 如何刷新页面而不出现此警告?

更新:我无法控制该项目! 我无法解决 POST 本身问题!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

冬天旳寂寞 2024-07-20 21:18:59

如果没有警告,您将无法刷新; 刷新指示浏览器重复上一个操作。 如果重复上一个操作涉及重新提交数据,则由浏览器选择是否警告用户。

您可以通过执行以下操作重新导航到具有新会话的同一页面:

window.location = window.location.href;

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;
少钕鈤記 2024-07-20 21:18:59

仅在 JavaScript 中更改 window.location危险,因为用户仍然可以点击后退按钮并重新提交帖子,这可能会产生意外结果(例如重复购买)。 PRG 是一个更好的解决方案

使用 Post/Redirect/Get (PRG) 模式

为了避免这个问题,许多 Web 应用程序使用 PRG 模式 - POST 操作不是直接返回 HTML 页面,而是返回重定向命令(使用 HTTP 303 响应代码(有时是 302)以及 HTTP“位置”响应) header),指示浏览器使用 HTTP GET 请求加载不同的页面。 然后可以安全地为结果页面添加书签或重新加载,而不会产生意外的副作用。

客户端

如果您想完全在客户端执行此操作,则需要在刷新之前更改浏览器历史记录:

if ( window.history.replaceState ) {
    window.history.replaceState( null, null, window.location.href );
}
window.location = window.location.href;

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.

Client Side

If you want to do it entirely client side, you'll need to change the browser history before you do the refresh:

if ( window.history.replaceState ) {
    window.history.replaceState( null, null, window.location.href );
}
window.location = window.location.href;
睫毛溺水了 2024-07-20 21:18:59

我遇到了一些锚点/哈希网址(包括 #)问题,无法使用 Rex 的解决方案重新加载...

所以我最终删除了哈希部分:

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

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];
农村范ル 2024-07-20 21:18:59

要绕过 POST 警告,您必须重新加载包含完整 URL 的页面。 工作正常。

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

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;
疯了 2024-07-20 21:18:59

您可以使用 JavaScript:

window.location.assign(document.URL);

在 Firefox 和 Chrome 上为我工作

检查此链接:http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get

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

伤痕我心 2024-07-20 21:18:59

window.location.replace(window.location.href); 怎么样?

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

暖伴 2024-07-20 21:18:59

这行得通

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

没有它就行不通的原因

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.

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.

嘦怹 2024-07-20 21:18:59

Nikl的版本没有传递get查询参数,我使用了以下修改后的版本:

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

或者在我的情况下我需要刷新最上面的页面\框架,所以我使用了以下版本

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

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;
无声无音无过去 2024-07-20 21:18:59

如果您正处于完成发布数据的阶段,并且只想重新查看页面,您可以只使用 window.location,甚至可以附加一个随机字符串作为查询参数,以保证新版本的页。

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.

冬天旳寂寞 2024-07-20 21:18:59
<html:form name="Form" type="abc" action="abc.do" method="get" onsubmit="return false;">

method="get" - 解决问题。

if method="post" 那么只会出现警告。

<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.

南笙 2024-07-20 21:18:59

我编写了一个函数,无需提交后即可重新加载页面,并且它也可以使用哈希值。

为此,我在名为 reload 的 URL 中添加/修改 GET 参数,并使用当前时间戳(以毫秒为单位)更新其值。

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;
};

请记住,如果您想在 href 属性中触发此函数,请按以下方式实现:href="javascript:reload();void 0;" 以使其成功工作。

我的解决方案的缺点是它会更改 URL,因此此“重新加载”不是真正的重新加载,而是使用不同查询的加载。 尽管如此,它仍然可以满足您的需求,就像它适合我一样。

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.

ぶ宁プ宁ぶ 2024-07-20 21:18:59

你不必被迫使用 javascript 来完成所有事情。 许多问题都有像这样的简单解决方案。 你可以使用这个棘手的锚点:

<a href=".">Continue</a>

当然我知道你可能需要一个自动解决方案,但这在很多情况下都会有所帮助。

祝你好运

you are not forced to use javascript to do every thing. Many problems have easy solutions like this one. you can use this tricky anchor:

<a href=".">Continue</a>

of course I know you may need an automatic solution but this will help in many cases.

good luck

泛泛之交 2024-07-20 21:18:59

在 html 中使用元刷新

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

在 php 中使用元刷新

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

using meta refresh in html

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

using meta refresh in php

echo "<meta http-equiv='refresh' content='1'>"
溺ぐ爱和你が 2024-07-20 21:18:59

这是一个应该始终有效且不会删除哈希的解决方案。

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;

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;
疯到世界奔溃 2024-07-20 21:18:59

使用 window.location 的其他解决方案对我不起作用,因为它们根本没有刷新它,所以我所做的是使用一个空表单将新的和空的 postdata 传递给同一页。 这是一种基于这个答案的方法:

function refreshAndClearPost() {
    var form = document.createElement("form");
    form.method = "POST";
    form.action = location.href;
    form.style.display = "none";

    document.body.appendChild(form);
    form.submit();    //since the form is empty, it will pass empty postdata
    document.body.removeChild(form);
}

The other solutions with window.location didn't work for me since they didn't make it refresh at all, so what I did was that I used an empty form to pass new and empty postdata to the same page. This is a way to do that based on this answer:

function refreshAndClearPost() {
    var form = document.createElement("form");
    form.method = "POST";
    form.action = location.href;
    form.style.display = "none";

    document.body.appendChild(form);
    form.submit();    //since the form is empty, it will pass empty postdata
    document.body.removeChild(form);
}
孤君无依 2024-07-20 21:18:59

使用即可。

location.reload(true);

只需在没有窗口的情况下

just use

location.reload(true);

without window.

听闻余生 2024-07-20 21:18:59

如果您使用 GET 方法而不是 POST ,那么我们无法获取表单字段值。 如果您使用 window.opener.location.href = window.opener.location.href; 那么我们可以触发数据库,​​我们可以获得值,但唯一的就是 JSP 即使 scriplet 具有表单值,也不会刷新。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文