问题
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.
Once the user has finished on the payment gateways page, the payment gateway will then do a form POST back to my SPA.
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.
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).
Is there a way to avoid this by using JavaScript, or is the PRG pattern the only way?
Note: It appears that changing window.location, document.location, as well as using history.replaceState all do not work (at least in Chrome).
回答1:
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.
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.
来源:https://stackoverflow.com/questions/19135515/avoiding-form-resubmission-in-spa-without-p-r-g