1

我在 React 组件中有以下 useEffect 钩子:

useEffect(() => {
    const handleUnload = function (event) {
        navigator.sendBeacon("https://webhook.site/8c3a42ce-c525-4a84-8783-f65175ed1297", JSON.stringify({ "foo": "bar" }));
    }

    window.addEventListener('beforeunload', handleUnload);

    return () => {
        window.removeEventListener('beforeunload', handleUnload);
    }

}, []);

URL 只是我确认正在使用 Postman 工作的一些测试服务器。但是,当我关闭浏览器时,没有收到请求。

我有两个问题:

  1. 为什么这不起作用?

  2. 有没有办法用 sendBeacon 发送一个 javascript 对象数组?

非常感谢

4

0 回答 0