Javascript 通过javascript重新加载页面:避免“回发”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3666159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:38:41  来源:igfitidea点击:

Reloading page through javascript: avoid the "Postback" warning

javascriptjquerypostbackreload

提问by Sam

I'm triggering a page reload through javascript, using the following:

我正在通过 javascript 触发页面重新加载,使用以下内容:

window.location.reload(true);

However, in some cases (of previous postback), the browser is giving me the following warning "To Display the webpage again, the web browser needs to resend the information you've previously submitted...".

但是,在某些情况下(以前的回发),浏览器会向我发出以下警告“要再次显示网页,Web 浏览器需要重新发送您之前提交的信息...”。

Is there any way of avoing this message and just doing the postback anyway, because this might be confusing for the users? If I need to reload the page by another means, so be it.

有没有什么办法可以避免这条消息并且只是进行回发,因为这可能会让用户感到困惑?如果我需要通过其他方式重新加载页面,那就这样吧。

采纳答案by mays

I don't think there is any way to do a postback without the message when you are doing the reload.

我认为在重新加载时没有任何方法可以在没有消息的情况下进行回发。

Btw, whats the reload for? If it is for submitting the POST information again, i suggest you manually submit the data through ajax or a hidden form submission using javascript.

顺便说一句,重装是为了什么?如果是再次提交POST信息,建议手动通过ajax提交数据或者使用javascript隐藏表单提交。

回答by Michael

if you want to refresh the page without including post data and not just postback try

如果您想刷新页面而不包含发布数据而不仅仅是回发,请尝试

window.location = window.location.href;

回答by jensgram

The request you're trying to do again is a POSTrequest, hence the warning. You can either make sure that you're only reloading resources fetched via GETrequests or - alternatively - submit a hidden <form>via the POSTmethod (and thus simulating a reload guaranteed not to be cached).

您再次尝试执行的POST请求是一个请求,因此是警告。您可以确保仅重新加载通过GET请求获取的资源,或者 - 或者 -<form>通过该POST方法提交隐藏(从而模拟重新加载,保证不会被缓存)。

As you probably already know the warning has to do with idempotence(the lack of side effects) within the HTTP verbs. GET(amongst others) are (should be, at least) considered idempotent, whereas POSTrequests allow for changes on the server. Therefore, the client should prompt the user to verify that s/he intends to perform the action again.

您可能已经知道警告与HTTP 动词中的幂等性(没有副作用)有关。GET(除其他外)(至少应该)被认为是幂等的,而POST请求允许在服务器上进行更改。因此,客户端应提示用户验证他/她是否打算再次执行该操作。

回答by balupton

The only way would be to stop it from rePOSTing the params. Use window.location = window.location

唯一的方法是阻止它重新发布参数。用 window.location = window.location

回答by weston

One way of preserving state data within a page is with fragment identifiers on the url, eg. #filter1, #filter2, and then using the handy hashchange plugin for jQuery to execute code (i.e., reload the relevant part of the page with fresh results) when the url fragment changes via the onhashchange event. It's also nice because the different states can be bookmarked and cached.

在页面中保留状态数据的一种方法是在 url 上使用片段标识符,例如。#filter1、#filter2,然后当 url 片段通过 onhashchange 事件更改时,使用方便的 jQuery 哈希更改插件来执行代码(即,重新加载页面的相关部分并使用新结果)。这也很好,因为可以为不同的状态添加书签和缓存。

http://benalman.com/projects/jquery-hashchange-plugin/

http://benalman.com/projects/jquery-hashchange-plugin/

回答by kilalile

After:

后:

mysql_query("insert into table (id_item, quantity, cost) values($m, '$amount', '$cost')");

Put:

放:

header("Location: index.php");

Then reload your page using jquery, and you won't get the "...web browser needs to resend the information..." warning.

然后使用 jquery 重新加载您的页面,您将不会收到“...网络浏览器需要重新发送信息...”警告。