Sign in to follow this  
mahks

How to access data sent by navigator.sendBeacon

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?

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

I have found that if you attach data to the URL, like navigator.sendBeacon('log?url_data=1',real_data)

 

The 'url_data' does get placed in $_GET, but the 'real_data' is I know not where.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.