避免在SPA中重新提交表单(无P-R-G) [英] Avoiding form resubmission in SPA (without P-R-G)
问题描述
我有一个用DurandalJS v1.2构建的单页应用程序(SPA),该应用程序与支付网关集成在一起. SPA将通过在其页面上执行常规形式POST转到付款网关的页面.
I have a Single Page Application (SPA) built with DurandalJS v1.2 that integrates with a payment gateway. The SPA will go off to the payment gateway's page by doing a normal form POST to their page.
用户完成支付网关"页面上的操作后,支付网关将执行POST表格返回我的SPA.
Once the user has finished on the payment gateways page, the payment gateway will then do a form POST back to my SPA.
由于它是POST,因此我在ASP.NET MVC中通过为发送的各种表单参数呈现require.js模块定义来处理它的服务器端,然后在需要处理以下内容的视图模型中使用此模块:结果.
Since it's a POST, I handle it server side in ASP.NET MVC by rendering out a require.js module definition for the various form parameters being sent, and this module is then used in the view model that needs to handle the result.
一切正常,我处理结果,然后保留在同一SPA页面上.这与Post-Redirect-Get的典型Web模式不同,因为SPA不执行任何重定向.
This is all working fine, I handle the result, and then stay on the same SPA page. This is different to the typical web pattern of Post-Redirect-Get, since the SPA doesn't do any redirects.
现在的问题是,当按下浏览器的刷新按钮时,它会弹出浏览器的重试帖子确认"对话框(我不想要).
The issue now is when pressing the browser refresh button, it's bringing up the browser's retry post confirmation dialog (which I don't want).
是否有使用JavaScript避免这种情况的方法,还是PRG模式是唯一的方法?
Is there a way to avoid this by using JavaScript, or is the PRG pattern the only way?
注意:似乎更改window.location,document.location以及使用history.replaceState都不起作用(至少在Chrome中是这样).
Note: It appears that changing window.location, document.location, as well as using history.replaceState all do not work (at least in Chrome).
推荐答案
如果您可以修改付款网关,然后拦截表单提交(<form onsubmit="...; return false">
),请处理JavaScript中的表单字段,而不是让浏览器将其发送给服务器.
If you can modify the payment gateway, then intercept the form submission (<form onsubmit="...; return false">
), process the form fields from JavaScript instead of letting the browser send them to the server.
如果您无法修改付款网关,请在回发后立即通过JavaScript修改document.location
(例如,添加#
).如果用户在此之后按下重新加载,浏览器将不会要求您确认.
If you can't modify the payment gateway, then modify the document.location
from JavaScript just after the POST back (e.g. by adding a #
). If the user presses reload after this, the browser won't ask for confirmation.
这篇关于避免在SPA中重新提交表单(无P-R-G)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!