Avoiding form resubmission in SPA (without P-R-G)
Asked Answered
M

1

1

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).

Madaras answered 2/10, 2013 at 11:7 Comment(1)
Coincidentally, if I do refresh the browser and accept the form re-submission, since I do actually change the hash before this occurs, the URL my SPA enters in on refresh isn't the one that processes the payment gateway responses, and so it doesn't handle it a second time. However, the confirmation dialog is still an annoyance.Madaras
G
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.

Gourmont answered 2/10, 2013 at 11:12 Comment(4)
It's being posted from another website, not on my page. I cannot modify the payment gateway, as it is a completely separate system.Madaras
I've added another idea.Gourmont
thank you for the suggestion, I failed to mention I'd already tried changing the hashMadaras
BTW, changing the hash does solve the problem of not processing the result twice, see comment on original post.Madaras

© 2022 - 2024 — McMap. All rights reserved.