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

Javascript 在POST后重新加载浏览器窗口,而不提示用户重新发送POST数据,javascript,Javascript,当用户访问我的网站时,每个页面上都有一个“登录”链接。单击此按钮将使用一些JavaScript显示一个覆盖窗口,提示用户输入其凭据。输入这些凭证后,将向web服务器发出Ajax调用以验证这些凭证;如果有效,则会发回身份验证票证cookie并重新加载页面,以便显示页面上特定于已验证用户或(现在)当前登录用户的任何内容 我使用以下脚本完成页面重新加载: window.location.reload(); 这对于通过GET请求加载的页面(绝大多数)非常有效,但有些页面使用回发表单。因此,如果用户转到

当用户访问我的网站时,每个页面上都有一个“登录”链接。单击此按钮将使用一些JavaScript显示一个覆盖窗口,提示用户输入其凭据。输入这些凭证后,将向web服务器发出Ajax调用以验证这些凭证;如果有效,则会发回身份验证票证cookie并重新加载页面,以便显示页面上特定于已验证用户或(现在)当前登录用户的任何内容

我使用以下脚本完成页面重新加载:

window.location.reload();
这对于通过GET请求加载的页面(绝大多数)非常有效,但有些页面使用回发表单。因此,如果用户转到其中一个页面,执行回发,然后选择登录,
window.location.reload()
脚本运行时,系统会提示用户是否要重新提交帖子正文

我想,为了解决这个问题,我可以告诉浏览器重新加载页面,所以我尝试:

window.location.href = window.location.href;
但我认为,浏览器不会对上述语句采取任何行动,因为它认为新URL与旧URL相同。如果我将上述内容更改为:

window.location.href = window.location.pathname;
它重新加载页面,但我丢失了所有查询字符串参数

我目前的解决方法已经足够了,但还不够完善-简而言之,我在当前的
window.location.href
值上添加了一个querystring参数,然后通过调用
reloadPage(“justLoggedIn”)
将其分配回
window.location.href
,其中
reloadPage
函数是:

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;
}
函数重载页面(querystringTackon){
var currentUrl=window.location.href;
如果(querystringTackon!=null&&querystringTackon.length>0&¤tUrl.indexOf(querystringTackon)<0){
if(currentUrl.indexOf(“?”)小于0)
currentUrl+=“?”+querystringTackon;
其他的
currentUrl+=“&”+querystringTackon;
}
window.location.href=currentUrl;
}
这是可行的,但它会导致URL为
www.example.com/SomeFolder/SomePage.aspx?justLoggedIn
,这似乎很俗气


JavaScript中是否有方法让它执行get重新加载,而不提示用户重新提交POST数据?我需要确保没有丢失任何现有的querystring参数。

您可能需要做的是在运行POST/Form处理程序脚本后重定向用户

在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();
?>

。。。这将允许您刷新页面,而不会收到“再次发送数据”警告


您甚至可以重定向到同一页面(如果您正在发布的是该页面),因为POST变量不会在标题中发送(因此不会在刷新时重新发布)

这是一篇旧文章,但我有一个更好的解决方案。创建一个包含所有post值作为隐藏字段的表单,并为表单命名,例如:

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

然后,在
setTimeout
中需要做的就是
RefreshForm.submit()


干杯

您可以尝试创建一个空表单,method=get,然后提交它

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

//要重新加载页面,请尝试
document.getElementById('reloader').submit();
不要问我为什么它有效……但它确实有效:)。
我想这就是js引擎解释逻辑的方式。

这也适用:

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

这也可以通过POST/REDIRECT/GET模式解决。
哪个更优雅:

我和你有同样的问题

以下是我所做的(动态生成的GET form,动作设置为location.href,隐藏的输入值为fresh),它似乎在所有浏览器中都能工作:

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();
var elForm=document.createElement(“表单”);
setAttribute(“方法”、“获取”);
setAttribute(“action”,window.location.href);
var elInputHidden=document.createElement(“输入”);
setAttribute(“类型”、“隐藏”);
elInputHidden.setAttribute(“名称”、“r”);
elInputHidden.setAttribute(“value”,new Date().getTime());
小精灵附肢子体(隐藏);
if(window.location.href.indexOf(“?”>)=0)
{
var_arrNameValue;
var strRequestVars=window.location.href.substr(window.location.href.indexOf(“?”)+1);
var_arrequestvariablepairs=strRequestVars.split(“&”);
对于(var i=0;i使用

而不是

document.location.reload(true); 

要重新加载具有所有参数的opener窗口(并非所有参数都使用window.location.href方法发送,而使用form.submit()方法可能要晚一步),我更喜欢使用window.history方法

window.opener.history.go(0);
如果您的URL中有哈希“#”,那么这里的所有解决方案都不起作用。这是唯一对我有效的解决方案

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就是这样做的。用它来代替你可以忽略的东西。这是一种黑客行为,找不到更好的解决方案

在Firefox中测试。

这对我很有效

window.location=window.location.pathname;

测试

  • 铬44.0.2403
  • IE边缘
  • 火狐39.0

您可以通过指定无卸载处理程序来利用该机制:

window.onbeforeunload = null;
window.location.replace(URL);
有关详细信息,请参阅WindowEventHandlers.onbeforeunload的

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

如果没有返回false;
,它将无法工作的原因是,单击按钮将触发表单提交。如果有显式返回false,它不会进行表单提交,只会重新加载上一次发布到该页面的同一页面。

当我们要从子页面刷新父页面时没有任何提示

代码如下:

window.opener.location.href=window.opener.location


这只是在没有任何提示的情况下刷新父页面。

怎么样:
window.location.href=window.location.href+'&reload=1';
?@jnpcl:这本质上就是我当前的解决方法(请参阅
reloadPage
fun
window.onbeforeunload = null;
window.location.replace(URL);
<button onclick="window.location.href=window.location.href; return false;">Continue</button>