dongzhengzhong1282 2017-04-08 22:33
浏览 88

为什么Post / Redirect / Get模式(PRG模式)有效?

I have read some articles about the PRG pattern and how it prevents users from resubmitting forms. For example, there is a good picture from this post:

enter image description here

I can understand why no form submission will happen when user refresh the page after 2xx has been received. But I am still wondering:

(1) What happens if the user refreshes the page before the redirect response coming back? At that time, the newest action in the browser is still the POST request, wouldn't that lead to resubmission?

(2) What happens if the user click "back"? Will this lead to resubmission?

  • 写回答

1条回答 默认 最新

  • dtf54486 2017-04-09 01:27
    关注

    (1) What happens if the user refreshes the page before the redirect response coming back? At that time, the newest action in the browser is still the POST request, wouldn't that lead to resubmission?

    There are 2 cases:

    CASE 1

    1. User requests form
    2. User submits form
    3. Server processes form and sends a redirect
    4. User decides to cancel the page before the redirect request has arrived

    In this case, the user cannot refresh the page because the refresh button is in Cancel mode. So the user must cancel and then refresh. So the user cancels the request and refreshes the page. The browser will issue the last GET request in the history which is:

    1. User requests form
    

    CASE 2

    1. User request the form
    2. User submits the form
    3. Server processes the form and sends a redirect
    4. The browser receives the redirect and issues a GET to the redirect URL
    5. While the server is processing the request, the user decides to cancel

    In this case the user cannot refresh the page and must cancel the request in order to refresh. So the user cancels the request. The user then refreshes, so the browser will issue the last GET request in the history which is:

    4. The browser receives the redirect and issues a GET to the redirect URL
    

    Here is the important part: POST requests do not remain in the browser history as mentioned here.

    (2) What happens if the user click "back"? Will this lead to resubmission?

    No, it will not. The submission can only happen if the user presses the form submission button.

    If you did not do the PRG pattern, then the browser will notice that upon clicking the back button a form submission may occur, it will prompt the user:

    The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?

    Or something similar depending on the browser.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)