There was an error while loading. Please reload this page.
1 parent fd3e0dc commit 8e37a25Copy full SHA for 8e37a25
NET网络请求/Fetch API/如何在关闭浏览器标签前,可靠地发送 HTTP 请求.html
@@ -60,6 +60,10 @@ <h3>解决方案二:fetch() 与 keepalive: true</h3>
60
灵活性高:相比 sendBeacon,它支持更多的 HTTP 方法(如 POST, PUT 等),并允许有限的请求头自定义。
61
API 统一:如果我们项目中已经大量使用 fetch,使用 keepalive 可以保持代码风格一致。
62
同样无法处理响应:和 sendBeacon 一样,由于页面已经关闭,我们无法在前端读取或处理服务器返回的响应。
63
+
64
+ <h3>如何选择解决方案?</h3>
65
+ 如果我们的需求是发送简单的分析或日志数据,navigator.sendBeacon() 是最直接、最符合语义的选择。
66
+ 如果我们需要更大的灵活性,比如使用 PUT 方法更新资源或需要设置特定的请求头,fetch({ keepalive: true }) 是更好的选择。
67
</pre>
68
</body>
69
<script>
0 commit comments