我有一个表单,在提交时使用$ .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
发布评论