POST后重新加载浏览器窗口,而不提示用户重新发送POST数据 [英] Reload browser window after POST without prompting user to resend POST data

查看:117
本文介绍了POST后重新加载浏览器窗口,而不提示用户重新发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When a user visits my website there is a "Login" link on every page. Clicking this uses some JavaScript to show an overlay window where the user is prompted for their credentials. After entering these credentials an Ajax call is made to the web server to validate them; if they are valid, an authentication ticket cookie is sent back and the page is reloaded so that any content on the page that is specific to authenticated users or the (now) currently logged in user is displayed.

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

I am accomplishing the page reload via script using:

window.location.reload();

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

This works wonderfully for pages loaded via a GET request (the vast majority), but some pages use postback forms. So if a user goes to one of these pages, performs a postback, and then chooses to login, when the window.location.reload() script runs they are prompted with the dialog box asking if they want to resubmit the POST body.

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

I thought to get around this I could just tell the browser to reload the page, so I tried:

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

但浏览器不会对上述声明采取任何行动,我认为因为它正在考虑新的网址与旧网址相同。如果我将上述内容更改为:

But the browser doesn't take any action with the above statement, I presume because it's thinking the new URL is the same as the old. If I change the above to:

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

重新加载页面,但我丢失了任何查询字符串参数。

It reloads the page, but I lose any querystring parameters.

我当前的解决方法已经足够了,但不是很好 - 简而言之,我将查询字符串参数添加到当前的 window.location.href 值,然后分配通过调用 reloadPage(justLoggedIn)返回 window.location.href ,其中 reloadPage 函数是:

My current workaround is sufficient, but not pretty - in short, I tack on a querystring parameter to the current window.location.href value and then assign that back to window.location.href by calling reloadPage("justLoggedIn"), where the reloadPage function is:

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

这样可行,但会导致网址 www.example.com/SomeFolder/SomePage.aspx?justLoggedIn ,这似乎很俗气。

This works, but it results in the URL being www.example.com/SomeFolder/SomePage.aspx?justLoggedIn, which seems tacky.

有没有办法在JavaScript中获取它执行GET重新加载而不提示用户重新提交POST数据?我需要确保任何现有的查询字符串参数都不会丢失。

Is there a way in JavaScript to get it to do a GET reload and not prompt the user to resubmit the POST data? I need to make sure that any existing querystring parameters are not lost.

推荐答案

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

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

Don't ask my why it works..but it does :). Just the way the js engine interprets the logic I suppose.

这篇关于POST后重新加载浏览器窗口,而不提示用户重新发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆