mahks Posted October 13, 2015 Share Posted October 13, 2015 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? Quote Link to comment Share on other sites More sharing options...
mahks Posted October 14, 2015 Author Share Posted October 14, 2015 Does no one use navigator.sendBeacon? Quote Link to comment Share on other sites More sharing options...
MattMcFarland Posted October 14, 2015 Share Posted October 14, 2015 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. Quote Link to comment Share on other sites More sharing options...
mahks Posted October 15, 2015 Author Share Posted October 15, 2015 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? Quote Link to comment Share on other sites More sharing options...
mahks Posted October 15, 2015 Author Share Posted October 15, 2015 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. Quote Link to comment Share on other sites More sharing options...
chg Posted October 15, 2015 Share Posted October 15, 2015 I googled "php post body" and this was the first result: http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php Quote Link to comment Share on other sites More sharing options...
mahks Posted October 15, 2015 Author Share Posted October 15, 2015 That had the answer !!! Thanks! The data from navigator.sendBeacon is in $HTTP_RAW_POST_DATA Quote Link to comment Share on other sites More sharing options...
MattMcFarland Posted October 15, 2015 Share Posted October 15, 2015 Glad you got it sorted Quote Link to comment Share on other sites More sharing options...
UsefulAngle Posted November 4, 2017 Share Posted November 4, 2017 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.