gpt4 book ai didi

javascript - 如何使 navigator.sendBeacon 使用 GET 方法

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:20 24 4
gpt4 key购买 nike

我想在客户的网站中使用 navigator.sendBeacon。但它使用的是 POST 方法,并且请求没有到达服务器,因为请求 url 的域不同。我尝试了使用 sendBeacon() 的不同方式,但都使用了 POST 方法。

1.

var data = new FormData();<br>
navigator.sendBeacon(myurl, data);
  1. navigator.sendBeacon(myurl, "");

  2. navigator.sendBeacon(myurl);

有没有办法使用 sendBeacon() 进行 GET 调用?或者有什么方法可以在跨域环境中使用sendBeacon()

最佳答案

来自W3C specification , 浏览器实现基于:

The sendBeacon() method does not provide ability to customize the request method. Applications that require non-default settings for such requests should use the FETCH API with keepalive flag set to true.

根据此文档,这里有一个 example关于如何使用 Fetch API 复制 sendBeacon 的行为:

fetch(url, {
method: ...,
body: ...,
headers: ...,
credentials: 'include',
mode: 'no-cors',
keep-alive: true,
})

关于javascript - 如何使 navigator.sendBeacon 使用 GET 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38027231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com