【问题标题】:Reloading the page after submitting POST in a browser在浏览器中提交 POST 后重新加载页面
【发布时间】:2018-02-06 06:45:58
【问题描述】:

我有一个使用 JQuery 对话框的 XPage,其中我有一个按钮(在纯 div 内没有 JQuery),它在触发时创建一个新的后端“部分”。另一方面,在客户端有验证(检查所有字段是否为空)。如果客户端验证成功,服务器端执行后端代码并创建新的“部分”。我遇到的问题是当创建新部件时,如果我按 F5 按钮,浏览器会提示我重复请求。如果用户单击“是”,则会创建新部分(这是表单中最后一个的副本)。我该如何预防?

<xp:button id="save_part_btn"
                        value="+Add part" style="float:right;">
                        <xp:eventHandler event="onclick" submit="true"
                            refreshMode="complete">
                        <xp:this.action><![CDATA[#{javascript:
var estdoc:NotesDocument=database.getDocumentByUNID(doc_source.getDocument().getParentDocumentUNID())
var estPartdoc:NotesDocument=estdoc.getParentDatabase().createDocument()

estPartdoc.replaceItemValue('Form','Estimate_Cost_Part')
estPartdoc.replaceItemValue('Predoc',estdoc.getUniversalID())
estPartdoc.replaceItemValue('$OSN_IsSaved','1')

estPartdoc.replaceItemValue('Title', getComponent('input_part_title').getValue())
estPartdoc.replaceItemValue('TSNB_Title',getComponent('input_tsnb_title').getValue())
estPartdoc.replaceItemValue('TSNB_All',getComponent('input_tsnb_all').getValue())
estPartdoc.replaceItemValue('TSNB_Build_Work',getComponent('input_tsnb_build_work').getValue())
estPartdoc.replaceItemValue('TSNB_Equipment',getComponent('input_tsnb_equipment').getValue())
estPartdoc.replaceItemValue('TSNB_Other_Costs',getComponent('input_tsnb_other_costs').getValue())
estPartdoc.replaceItemValue('TSNB_PIR',getComponent('input_tsnb_pir').getValue())
estPartdoc.replaceItemValue('TSNB_Return',getComponent('input_tsnb_return').getValue())

estPartdoc.replaceItemValue('Current_Title',getComponent('input_current_title').getValue())
estPartdoc.replaceItemValue('Current_All',getComponent('input_current_all').getValue())
estPartdoc.replaceItemValue('Current_Build_Work',getComponent('input_current_build_work').getValue())
estPartdoc.replaceItemValue('Current_Equipment',getComponent('input_current_equipment').getValue())
estPartdoc.replaceItemValue('Current_Other_Costs',getComponent('input_current_other_costs').getValue())
estPartdoc.replaceItemValue('Current_PIR',getComponent('input_current_pir').getValue())
estPartdoc.replaceItemValue('Current_NDS',getComponent('input_current_nds').getValue())
estPartdoc.replaceItemValue('Current_Return',getComponent('input_current_return').getValue())

//var estPartOdoc=new OsnovaUI_document(estPartdoc)
//estPartOdoc.makeDependent(estdoc)

print('new part created with id:'+estPartdoc)
estPartdoc.makeResponse(estdoc)
estPartdoc.save() 


var ag:NotesAgent=database.getAgent('User_CalculateEstimateCost')
ag.runOnServer(doc_source.getDocument().getNoteID())

}]]></xp:this.action>

服务器端代码,没什么特别的。 在submit = "true" 上,如果验证成功,则返回 true,如果失败,则返回 false

【问题讨论】:

  • 一些代码会有帮助

标签: javascript forms post xpages


【解决方案1】:

您需要实现 POST-Redirect-GET 模式。这是一个示例(来自 2010 年):XPages: Avoid saving duplicate documents on browser refresh

关键部分是在服务器端逻辑的末尾包含重定向。这是一个简单的例子:

context.redirectToPage(view.getPageName());

在您的情况下,重定向需要重新打开文档,请执行以下操作:

context.redirectToPage(view.getPageName()+ "?OpenXpage&documentId="+doc_source.getNoteID());

【讨论】:

  • 我想这种方式可行,但 onpageLoad 我使用getDocumentByUNID 但它给了我例外。有没有别的办法?
  • 请将您的 onPageLoad 逻辑添加到您的问题中。
  • var logdoc:NotesDocument=doc_source.getDocument() var objdoc:NotesDocument=logdoc.getParentDatabase().getDocumentByUNID(logdoc.getParentDocumentUNID())第2行出现异常
  • 所以使用context.redirectToPage(view.getPageName());时doc_source数据源无效。您需要将重定向与您的案例相匹配。这是另一个可能有效的示例: context.redirectToPage(view.getPageName()+ "?action=openDocument&documentId="+doc_source.getNoteID());
  • context.redirectToPage(view.getPageName()+ "?OpenXpage&amp;documentId="+doc_source.getNoteID()); 工作!非常感谢!
猜你喜欢
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2011-02-16
  • 2015-07-04
  • 2013-08-18
  • 2011-08-07
相关资源
最近更新 更多