Preventing form resubmission

前端 未结 12 953

Page one contains an HTML form. Page two - the code that handles the submitted data.

The form in page one gets submitted. The browser gets redirected to page two. P

12条回答
  •  长发绾君心
    2020-11-22 09:46

    You need to use PRG - Post/Redirect/Get pattern and you have just implemented the P of PRG. You need to Redirect. (Now days you do not need redirection at all. See this)

    PRG is a web development design pattern that prevents some duplicate form submissions which means, Submit form (Post Request 1) -> Redirect -> Get (Request 2)

    Under the hood
    

    Redirect status code - HTTP 1.0 with HTTP 302 or HTTP 1.1 with HTTP 303

    An HTTP response with redirect status code will additionally provide a URL in the location header field. The user agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request to the new URL specified in the location field.

    The redirect status code is to ensure that in this situation, the web user's browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted.

    Double Submit Problem
    

    Post/Redirect/Get Solution
    

    Source

提交回复
热议问题