Home > Net >  The cause of resubmit form in browser
The cause of resubmit form in browser

Time:07-04

I am newbie of backend developer. I am learning PRG (post redirect get) pattern to prevent the resubmit form when user click reload button after submitting the form. I don't understand why click reload button after submitting form cause resubmitting again.Please, tell me the causes of that problem.

CodePudding user response:

When you submit a form with method="POST", the browser constructs a POST request and sends it to the server. If the server responds with 200 OK and an HTML document then the browser will render that HTML document.

The refresh button is a browser feature which lets the user repeat the last request.

Since the last request, in the above scenario, is "a POST request", it causes the browser to repeat the POST request.


In the PRG pattern, the response to the POST request is a redirect which causes the browser to make a GET request.

In this scenario, the the request is "a GET request".

CodePudding user response:

Look at it like that:

When you submit the form, then the browser receives a response. If you do not redirect the browser, then the response is the result of submitting the form, right?

Now if you ask the browser to reload, so to repeat the last action, then what action does the browser have to repeat to reload the current response? Obviously the form submission, right? There is nothing else it can do, since that was how it gained the current response.

In contrast if you redirected the browser, then the browser requests where it has been redirected to. And if you ask it to reload, then it will retry that action, a request to where it has been redirected to. Which is a simple GET request, not a form submission.

  • Related