javascript 如何在不重新发送信息的情况下刷新网页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5312229/
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-10-25 16:44:51  来源:igfitidea点击:

How can I refresh a web page without resending the information?

c#javascriptasp.netpostbackrefresh

提问by Jeff Norman

I have an asp.net application and when I refresh a web page which is produced after a form is submitted.

我有一个 asp.net 应用程序,当我刷新提交表单后生成的网页时。

Example:

例子:

<form name="myform" action="mypage.aspx" method="post">

<form name="myform" action="mypage.aspx" method="post">

then the following alert is shown.

然后显示以下警报。

"The page cannot be refreshed without resending the information. Click Retry to Send the information again, or click Cancel to return to the page that you were trying to view."

“不重新发送信息就无法刷新页面。单击重试再次发送信息,或单击取消返回到您试图查看的页面。”

With two buttons:

有两个按钮:

RetryCancel

RetryCancel

How can I avoid the above alert and take "Cancel" as default & refresh immediately?

如何避免上述警报并将“取消”作为默认值并立即刷新?

Thank you

谢谢

Jeff

杰夫

回答by Richard H

You should use the "Redirect-After-Post" pattern. Google for more info. But basically on form submission your server sends a redirect response to the client, which then fetches this page. This is the best practice for avoiding the issue you are seeing: you don't get that annoying message on page refresh, the back button is not "broken" and it prevents accidental double submission. Really everyform on your site should implement this pattern.

您应该使用“Redirect-After-Post”模式。谷歌了解更多信息。但基本上在表单提交时,您的服务器向客户端发送重定向响应,然后客户端获取此页面。这是避免您所看到的问题的最佳实践:您不会在页面刷新时收到烦人的消息,后退按钮没有“损坏”并且可以防止意外的双重提交。实际上,您网站上的每个表单都应该实现这种模式。

Here are a couple of questions on implementing it in asp.net:

以下是在 asp.net 中实现它的几个问题:

How to use ASP.NET with "Redirect after POST" pattern? [Edited]

如何使用具有“POST 后重定向”模式的 ASP.NET?[编辑]

Redirect After Post in ASP.NET MVC

在 ASP.NET MVC 中发布后重定向

回答by Brad Christie

As I mentioned in a comment, you can store the last submission in a variable (such as session), then use a redirect to re-visit the page. A "refresh" classically is just going to perform the last option, so now way of "refreshing without a submit".

正如我在评论中提到的,您可以将最后一次提交存储在一个变量中(例如会话),然后使用重定向重新访问该页面。经典的“刷新”只是执行最后一个选项,所以现在“刷新而不提交”的方式。

But, depending on what you're trying to achieve, you could also use an UpdatePaneland "Refresh" the page (or, for all intents and purposes, make it appear as though it's refreshed).

但是,根据您要实现的目标,您还可以使用UpdatePanel并“刷新”页面(或者,出于所有意图和目的,使其看起来好像已刷新)。

EDIT:If the user presses F5, then prevent that action (if it's imperative). Perform whatever actions you need on the initial page visit, then auto-redirect them to a "Result" page. That way any refresh just refreshes a "static" (already-generated/action-performed) page.

编辑:如果用户按 F5,则阻止该操作(如果它是必要的)。在初始页面访问时执行您需要的任何操作,然后将它们自动重定向到“结果”页面。这样,任何刷新只会刷新“静态”(已生成/已执行)页面。

回答by Dave

Jeff,

杰夫,

I had this same issue yesterday with a multi-paged form (4 seperate steps). What I did in the end was created a "handler" file and used that to deal with posted data, then performed a redirect to the page I wanted. This counters the "resend" problem as data is only ever posted to a page that is dedicated to processing it.

昨天我在多页表单(4 个单独的步骤)中遇到了同样的问题。我最后所做的是创建一个“处理程序”文件并用它来处理发布的数据,然后重定向到我想要的页面。这可以解决“重新发送”问题,因为数据只会发布到专用于处理它的页面。

My HTML Form:

我的 HTML 表单:

<form action="handler.asp" method="post">
    <input type="text" name="firstName" />
    <input type="submit" value="Send Data" />
    <input type="hidden" name="action" value="displayName" />
</form>

In my handler.asp:

在我的 handler.asp 中:

<%

    if Request.Form("action") = "displayName" Then

        ' Do whatever is needed here.
        ' After you're done
        ' Send the user where you want them to go

        Response.Redirect("default.asp?status=thanks")

    End If

%>

I hope this is useful. Good luck

我希望这是有用的。祝你好运

回答by Theofanis Pantelides

Jeff,

杰夫,

To expand on the above comments:

要扩展上述评论:

Either switch the

要么切换

method="post"

to:

到:

method="get"

... or do this manually, in a way that does not compromise the security of ur data.

...或手动执行此操作,以不损害您数据安全性的方式。

Say you have a login for with two textboxes user/pass. You could create a struct/class with this information, and then generate a random GUID.

假设您有两个文本框用户/密码的登录名。您可以使用此信息创建一个结构/类,然后生成一个随机 GUID。

do something like:

做类似的事情:

Session[GUID] = new MyPostInformationClass() {login = txtLogin.Text, pass = txtPass.Text};

and then:

接着:

Response.Redirect("mypage.aspx?guid=" + GUID);

then on the Page_Load:

然后在 Page_Load 上:

MyPostInformationClass usr = (MyPostInformationClass)Session[GUID];
String login = usr.login;
String pass = usr.pass;

Of course this is an overly simplified example; but you can F5 you heart's desire.

当然,这是一个过于简化的例子;但是你可以F5你内心的渴望。