如果我刷新页面,POST不会完成(POST doesn't complete if I refresh page)

编程入门 行业动态 更新时间:2024-10-23 21:36:11
如果我刷新页面,POST不会完成(POST doesn't complete if I refresh page)

我有一个表单,在提交时使用$ .ajax来调用将数据写入文本文件的bash脚本。

在Firefox中,如果我立即刷新页面或导航到其他页面,POST将中止,文本文件不会更新:

[15:07:12.781] POST http://server/writegit.cgi [undefined 891ms]

如果我在刷新之前等待一两秒钟,POST就完成了,一切都很好:

[15:03:04.995] POST http://server/writegit.cgi [HTTP / 1.1 200 OK 2022ms]

但是,Chrome中不会出现此问题 - 一旦表单提交,即使我立即刷新页面,POST也会完成。

有什么方法可以让Firefox像Chrome一样?

I have a form which uses $.ajax on submit to call a bash script that writes data to a text file.

In Firefox, if i immediately refresh the page or navigate to a different page, the POST aborts, and the text file does not update:

[15:07:12.781] POST http://server/writegit.cgi [undefined 891ms]

If I wait a second or two before refreshing, the POST completes and everything is fine:

[15:03:04.995] POST http://server/writegit.cgi [HTTP/1.1 200 OK 2022ms]

However, this problem does not happen in Chrome-- once the form submits, the POST will complete even if I refresh the page immediately.

Is there any way I can make Firefox behave like Chrome?

最满意答案

Ajax调用是异步的 - 意味着它们会被激活,并且将来某个时候会响应。 代码不等待响应。 如果您在此之前离开页面,则从请求下拉出地毯。

要么使调用同步,要么添加一些“onbeforeunload”事件处理程序以检查您的ajax调用是否已完成,然后再允许用户离开页面。

或者:只是不刷新页面!

Ajax calls are asynchronous - meaning they fire off and sometime in the future a response will come. The code does not wait for the response. If you navigate away from the page before this happens, you pull the rug out from under the request.

Either make the call synchronous, or add some "onbeforeunload" event handler to check if your ajax call has completed before allowing the user to navigate away from the page.

or: just don't refresh the page!

更多推荐

POST,Firefox,刷新,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-05 18:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   POST   complete   refresh   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!