我目前在 HTTP post 表单和历史推送状态方面遇到了一个小问题。
当我的用户在 example.com/page-1
中提交 POST 表单,然后单击此页面中的链接时,他们会被重定向到 example.com/page-2
带有history.pushstate。 JS 脚本只是更改当前的 url,将旧的内容推送到历史记录,然后显示新的内容。这对我来说非常有效。
问题:用户访问 example.com/page-2
并手动刷新页面(ctrl+r 或刷新按钮)时,Chrome 会提示用户再次提交表格;但该表单实际上位于 example.com/page-1
中,并且没有理由在这个"new"页面中再次询问。
有没有办法在 popstate 更改时清除 javascript 中所有先前提交的数据,以防止浏览器重新发布提示?或者我错过了我的历史状态的某些内容?或者浏览器目前还没有很好地实现它?
我只使用 Chrome 进行了测试,因为我没有使用其他浏览器实现 Pushstate。
最佳答案
也许可以尝试替换状态?请参阅this .
The
replaceState()
methodhistory.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.
replaceState() is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.
关于forms - History.pushstate 和 HTTP POST 表单转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13649633/