如何在 Javascript 中重新加载页面而不出现 POSTDATA 警告?
我想使用以下命令重新加载页面:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
如果没有警告,您将无法刷新; 刷新指示浏览器重复上一个操作。 如果重复上一个操作涉及重新提交数据,则由浏览器选择是否警告用户。
您可以通过执行以下操作重新导航到具有新会话的同一页面:
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:
仅在 JavaScript 中更改
window.location
是危险,因为用户仍然可以点击后退按钮并重新提交帖子,这可能会产生意外结果(例如重复购买)。 PRG 是一个更好的解决方案使用 Post/Redirect/Get (PRG) 模式
客户端
如果您想完全在客户端执行此操作,则需要在刷新之前更改浏览器历史记录:
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 solutionUse the Post/Redirect/Get (PRG) pattern
Client Side
If you want to do it entirely client side, you'll need to change the browser history before you do the refresh:
我遇到了一些锚点/哈希网址(包括 #)问题,无法使用 Rex 的解决方案重新加载...
所以我最终删除了哈希部分:
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:
要绕过 POST 警告,您必须重新加载包含完整 URL 的页面。 工作正常。
To bypass POST warning you must reload page with full URL. Works fine.
您可以使用 JavaScript:
在 Firefox 和 Chrome 上为我工作
检查此链接:http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get
You can use JavaScript:
Worked for me on Firefox and Chrome
check this link: http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get
window.location.replace(window.location.href); 怎么样?
how about window.location.replace(window.location.href);
这行得通
没有它就行不通的原因
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
The reason it didn't work without the
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.
Nikl的版本没有传递get查询参数,我使用了以下修改后的版本:
或者在我的情况下我需要刷新最上面的页面\框架,所以我使用了以下版本
Nikl's version doesn't pass get query parameters, I used the following modified version:
or in my case I needed to refresh the topmost page\frame, so I used the following version
如果您正处于完成发布数据的阶段,并且只想重新查看页面,您可以只使用 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.
method="get"
- 解决问题。if method="post"
那么只会出现警告。method="get"
- resolves the problem.if method="post"
then only warning comes.我编写了一个函数,无需提交后即可重新加载页面,并且它也可以使用哈希值。
为此,我在名为
reload
的 URL 中添加/修改 GET 参数,并使用当前时间戳(以毫秒为单位)更新其值。请记住,如果您想在 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.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.
你不必被迫使用 javascript 来完成所有事情。 许多问题都有像这样的简单解决方案。 你可以使用这个棘手的锚点:
当然我知道你可能需要一个自动解决方案,但这在很多情况下都会有所帮助。
祝你好运
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:
of course I know you may need an automatic solution but this will help in many cases.
good luck
在 html 中使用元刷新
在 php 中使用元刷新
using meta refresh in html
using meta refresh in php
这是一个应该始终有效且不会删除哈希的解决方案。
Here's a solution that should always work and doesn't remove the hash.
使用
window.location
的其他解决方案对我不起作用,因为它们根本没有刷新它,所以我所做的是使用一个空表单将新的和空的 postdata 传递给同一页。 这是一种基于这个答案的方法: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:使用即可。
只需在没有
窗口
的情况下just use
without
window
.如果您使用
GET
方法而不是POST
,那么我们无法获取表单字段值。 如果您使用window.opener.location.href = window.opener.location.href;
那么我们可以触发数据库,我们可以获得值,但唯一的就是JSP
即使 scriplet 具有表单值,也不会刷新。If you use
GET
method instead ofPOST
then we can't the form filed values. If you usewindow.opener.location.href = window.opener.location.href;
then we can fire the db and we can get the value but only thing is theJSP
is not refreshing eventhough the scriplet having the form values.