最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make navigator.sendBeacon use GET method - Stack Overflow

programmeradmin0浏览0评论

I want to use navigator.sendBeacon in a client's website. But it is using the POST method and the request is not reaching the server as the request url's domain is different. I tried different ways of using sendBeacon(), but all are using the POST method.

1.

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

  2. navigator.sendBeacon(myurl);

Is there a way to make GET call using sendBeacon()? Or is there any way to use sendBeacon() in a cross-domain environment.

I want to use navigator.sendBeacon in a client's website. But it is using the POST method and the request is not reaching the server as the request url's domain is different. I tried different ways of using sendBeacon(), but all are using the POST method.

1.

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

  2. navigator.sendBeacon(myurl);

Is there a way to make GET call using sendBeacon()? Or is there any way to use sendBeacon() in a cross-domain environment.

Share Improve this question edited Sep 8, 2021 at 15:15 Dan Atkinson 11.7k14 gold badges88 silver badges116 bronze badges asked Jun 25, 2016 at 10:00 madhusudhanmadhusudhan 3372 gold badges5 silver badges12 bronze badges 1
  • maybe is a CORS problem? – Cristian Sepulveda Commented Aug 11, 2017 at 4:33
Add a comment  | 

3 Answers 3

Reset to default 12

From the W3C specification, on which browser implementations are based:

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.

Per this documentation, here's an example of how the Fetch API can be used to replicate the behavior of sendBeacon:

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

Although navigator.sendBeacon uses POST you can still pass the data as a ?query=string and this will reach the URL endpoint.

You can then simply parse the URL on the server and extract the data that way.

I use this for approach to debug production sites when I want to keep the DevTools closed but still see messages in my local terminal. Here is the output...

navigator.sendBeacon("http://127.0.0.1:8000/?"+string, string);

127.0.0.1 - - [28/Jan/2021 21:26:43] code 501, message Unsupported method ('POST')
127.0.0.1 - - [28/Jan/2021 21:26:43] "POST /?window-hidden HTTP/1.1" 501 -
127.0.0.1 - - [28/Jan/2021 21:27:42] code 501, message Unsupported method ('POST')
127.0.0.1 - - [28/Jan/2021 21:27:42] "POST /?window%20focus HTTP/1.1" 501 -
127.0.0.1 - - [28/Jan/2021 21:27:42] code 501, message Unsupported method ('POST')
127.0.0.1 - - [28/Jan/2021 21:27:42] "POST /?window%20blur HTTP/1.1" 501 -
127.0.0.1 - - [28/Jan/2021 21:27:42] code 501, message Unsupported method ('POST')
127.0.0.1 - - [28/Jan/2021 21:27:42] "POST /?window%20-%20hidden HTTP/1.1" 501 -

It seems that there is no full standardization of how browsers interpret sendBeacon(); some by default send on $_GET, and some on $_POST. they should use a 3rd argument to let the developer use GET or POST it would be more clear

发布评论

评论列表(0)

  1. 暂无评论