Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[html] 第52天 From表单提交时为什么会刷新页面?怎么预防刷新? #202

Open
haizhilin2013 opened this issue Jun 6, 2019 · 2 comments

Comments

@haizhilin2013
Copy link
Collaborator

第52天 From表单提交时为什么会刷新页面?怎么预防刷新?

@liuxiaole
Copy link

因为早期网页交互模型只能是浏览器提交数据给服务器,服务器做出响应重新返回一个页面,浏览器加载这个页面进行显示。早期前端没有编程式发送网络请求的 API,更没有前端路由管理的概念,所以表单提交跳转页面是广泛接受的方案。

不想刷新可以使用 JS 拦截 form 的 onsubmit 事件,阻止掉浏览器的默认行为,然后用 ajax/fetch 和后台交互。另一个偏方是使用 iframe 作为 form 的 target,不过 JS 处理方面不如让浏览器别管自己全手动发请求来得简单。

@MrZ2019
Copy link

MrZ2019 commented Nov 23, 2020

因为早期网页交互模型只能是浏览器提交数据给服务器,服务器做出响应重新返回一个页面,浏览器加载这个页面进行显示。早期前端没有编程式发送网络请求的 API,更没有前端路由管理的概念,所以表单提交跳转页面是广泛接受的方案。

不想刷新可以使用 JS 拦截 form 的 onsubmit 事件,阻止掉浏览器的默认行为,然后用 ajax/fetch 和后台交互。另一个偏方是使用 iframe 作为 form 的 target,不过 JS 处理方面不如让浏览器别管自己全手动发请求来得简单。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants