Jump to content

How to access data sent by navigator.sendBeacon


mahks
 Share

Recommended Posts

Documentation indicates sendBeacon sends its data via a HTTP POST request, but I cannot find the data sent.

Here is my javascript code : navigator.sendBeacon('log.php','Lost Data')

 

Browser logs indicate the "Lost Data" is the "request payload" for the request

 

In PHP the request is received but the $_POST variable seems to be an empty array.

 

I have sent from both Firefox & Chrome to both my local test server (windows) and to the live server (unix), all react the same.

What am I doing wrong?

Link to comment
Share on other sites

I ran some tests for you..

 

https://beacon-mattmcfarland.c9.io/index.html

 

The difference between the ajax post and the send beacon is the request itself:

 

The ajax post is sending Form Data, while the sendbeacon sends "Request Payload"

 

I do not know the implications of this on PHP, but the server will definitely see these differently.

 

You may have to send headers along with it that tell the server it is sending a www-url-encoded form.

Link to comment
Share on other sites

Hey thanks for the effort!

 

All documentation I have found on sendbeacon does not seem to indicate headers need configuring, just navigator.sendBeacon(file,data) is needed.

But none of the documentation and none of the examples given on dozens of sites I've looked at ever show the PHP side!

 

I assume that is because it is obvious (to them) where the data lands.

 

Would really like to hear from someone that has/is using this ... please what am I missing that is so obvious?

Link to comment
Share on other sites

  • 2 years later...

Depending on the Content-Type HTTP header of the Beacon request, you can access data on the server. If you want to use the normal $_POST on your server, the Content-Type should be "application/x-www-form-urlencoded" or "multipart/form-data".

The Beacon API uses the type of transmitted data to set the Content-Type of the request.  For example this will send a "multipart/form-data" request to the server :

var fd = new FormData(); 
fd.append('ajax_data', 22); 
navigator.sendBeacon('ajax.php', fd);

You can access this on the server :

<?php

// perform action with $_POST['ajax_data']

?>

 

More examples for setting Content-Type header can be found here :  http://usefulangle.com/post/63/javascript-navigator-sendbeacon-set-form-http-header

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...