在POST后重新加载浏览器窗口,而不提示用户重新发送POST数据

当用户访问我的网站时,每个页面上都有一个“login”链接。 点击这个使用一些JavaScript来显示一个覆盖窗口,用户被提示input他们的凭证。 input这些证书之后,Ajax会调用Web服务器来validation它们; 如果它们是有效的,则authentication票据cookie被发送回来并且页面被重新加载,使得页面上特定于经authentication的用户或(当前)当前login的用户的任何内容被显示。

我正在通过使用脚本完成页面重新加载:

window.location.reload(); 

这对于通过GET请求(绝大多数)加载的页面非常有效,但有些页面使用回发表单。 因此,如果用户转到其中一个页面,执行回发,然后selectlogin,那么当window.location.reload()脚本运行时,会提示对话框询问他们是否要重新提交POST正文。

重新提交POST正文对话框

我想绕过这个我可以告诉浏览器重新加载页面,所以我试着:

 window.location.href = window.location.href; 

但浏览器不采取任何行动与上述声明,我认为,因为它认为新的URL是旧的。 如果我将上面的内容更改为:

 window.location.href = window.location.pathname; 

它重新加载页面,但我失去了任何querystring参数。

我目前的解决方法是足够的,但不漂亮 – 总之,我把一个querystring参数reloadPage("justLoggedIn")到当前的window.location.href值,然后通过调用reloadPage("justLoggedIn")将其分配回window.location.href ,其中reloadPagefunction是:

 function reloadPage(querystringTackon) { var currentUrl = window.location.href; if (querystringTackon != null && querystringTackon.length > 0 && currentUrl.indexOf(querystringTackon) < 0) { if (currentUrl.indexOf("?") < 0) currentUrl += "?" + querystringTackon; else currentUrl += "&" + querystringTackon; } window.location.href = currentUrl; } 

这是有效的,但它导致URL为www.example.com/SomeFolder/SomePage.aspx?justLoggedIn ,这似乎俗气。

有没有在JavaScript的方法来让它做一个GET重新加载,而不是提示用户重新提交POST数据? 我需要确保任何现有查询string参数都不会丢失。

 window.location.href = window.location.href; 

不要问我为什么工作..但它确实:)。 就像js引擎解释我所想的逻辑一样。

你可能需要做的是在POST / Form Handler脚本运行后redirect用户。

在PHP中,这样做是这样的…

 <?php // ... Handle $_POST data, maybe insert it into a database. // Ok, $_POST data has been handled, redirect the user header('Location:success.php'); die(); ?> 

…这应该允许您刷新页面,而不会收到“发送数据”警告。

你甚至可以redirect到同一个页面(如果这就是你发布的内容),因为POSTvariables将不会在头部发送(因此不会在刷新时重新POST)

这也起作用:

 window.location.href = window.location.pathname + window.location.search 

使用

 RefreshForm.submit(); 

代替

 document.location.reload(true); 

我和你有同样的问题。

这是我做的(dynamic生成GET表单,动作设置为location.href,具有新值的隐藏input),它似乎适用于所有浏览器:

 var elForm=document.createElement("form"); elForm.setAttribute("method", "get"); elForm.setAttribute("action", window.location.href); var elInputHidden=document.createElement("input"); elInputHidden.setAttribute("type", "hidden"); elInputHidden.setAttribute("name", "r"); elInputHidden.setAttribute("value", new Date().getTime()); elForm.appendChild(elInputHidden); if(window.location.href.indexOf("?")>=0) { var _arrNameValue; var strRequestVars=window.location.href.substr(window.location.href.indexOf("?")+1); var _arrRequestVariablePairs=strRequestVars.split("&"); for(var i=0; i<_arrRequestVariablePairs.length; i++) { _arrNameValue=_arrRequestVariablePairs[i].split("="); elInputHidden=document.createElement("input"); elInputHidden.setAttribute("type", "hidden"); elInputHidden.setAttribute("name", decodeURIComponent(_arrNameValue.shift())); elInputHidden.setAttribute("value", decodeURIComponent(_arrNameValue.join("="))); elForm.appendChild(elInputHidden); } } document.body.appendChild(elForm); elForm.submit(); 

这对我有效。

window.location = window.location.pathname;

testing

  • Chrome 44.0.2403
  • IE边缘
  • Firefox 39.0

这是一个较旧的post,但我有一个更好的解决scheme。 创build一个包含您的所有post值的表单作为隐藏的字段,并给表单一个名称,如:

 <form name="RefreshForm" method="post" action="http://yoursite/yourscript"> <input type="hidden" name="postVariable" value="PostData"> </form> 

然后,你需要在你的setTimeout做的是RefreshForm.submit();

干杯!

这也可以通过POST / REDIRECT / GET模式解决。
哪个更优雅?
如何重新加载一个没有POSTDATA警告的页面?

你可以尝试创build一个空的表单,method = get,并提交它。

 <form id='reloader' method='get' action="enter url here"> </form> <script> // to reload the page, try document.getElementById('reloader').submit(); </script> 

重新加载opener窗口的所有参数(不是所有的发送与window.location.href方法和form.submit()方法可能是一步晚)我喜欢使用window.history方法。

 window.opener.history.go(0); 

如果您的URL中有散列符“#”,则此处的所有解决scheme都不起作用。 这是为我工作的唯一解决scheme。

 var hrefa = window.location.href.split("#")[1]; var hrefb = window.location.href.split("#")[2]; window.location.href = window.location.pathname + window.location.search + '&x=x#' + hrefa + '#' + hrefb; 

如果你有一个散列,URL必须是不同的重新加载。 &x = x在这里做。 只要用这个替代你可以忽略的东西。 这是一个黑客技巧,无法find一个更好的解决scheme..

在Firefox中testing

您可以利用HTML提示来卸载机制,方法是指定no unload handler:

 window.onbeforeunload = null; window.location.replace(URL); 

有关更多信息, 请参阅 WindowEventHandlers.onbeforeunload 的注释部分 。