xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Beacon API & multipart/form-data & JSON

Beacon API & multipart/form-data & JSON

https://stackoverflow.com/questions/45847276/navigator-sendbeacon-with-application-x-www-form-urlencoded

Blob to JSON



demos


// const wxRobot = (params = {}, desc = '') => {
//   // 测试 robot, Beacon API
//   const url = 'http://localhost:3000/api/beacon';
//   const obj = {
//       msgtype: 'markdown',
//       markdown: {
//           content: `
// desciption: ${desc}\n
// data: ${JSON.stringify(params, null, 4)}\n
//           `,
//       },
//   };
//   // 1. Query String
//   const options = new URLSearchParams({
//       key : 'id',
//       values : '123',
//   });
//   navigator.sendBeacon(url, options);
// };

// const wxRobot = (params = {}, desc = '') => {
//   // 测试 robot, Beacon API
//   const url = 'http://localhost:3000/api/beacon';
//   const obj = {
//       msgtype: 'markdown',
//       markdown: {
//           content: `
// desciption: ${desc}\n
// data: ${JSON.stringify(params, null, 4)}\n
//           `,
//       },
//   };
//   // 2. FormData String
//   let data = new FormData();
//   for (const key of Object.keys(obj)) {
//       data.append(key, obj[key]);
//   }
//   if(desc) {
//       data.append('desc', desc);
//   }
//   navigator.sendBeacon(url, data);
// };

const wxRobot = (params = {}, desc = '') => {
  // 测试 robot, Beacon API
  const url = 'http://localhost:3000/api/beacon';
  const obj = {
      msgtype: 'markdown',
      markdown: {
          content: `
desciption: ${desc}\n
data: ${JSON.stringify(params, null, 4)}\n
          `,
      },
  };
  // 3. JSON Object
  const options = new Blob(
      [JSON.stringify(obj)],
      {type : 'application/json'}
  );
  navigator.sendBeacon(url, options);
};

// wxRobot(params, desc);

/*

const params = {
    "user": "Eric",
    "os": "macOS",
    "browser": "Chrome",
};
wxRobot(params, '✅ beacon api & json');

*/

Beacon API

https://cdn.xgqfrms.xyz/Beacon-API/beacon-api-json.html

Content-Type header that Beacon API use, depend on type of instance you pass to sendBeacon second parameter.

application/x-www-form-urlencoded
To send application/x-www-form-urlencoded, use UrlSearchParams instance.

// 1. Query String
const params = new URLSearchParams({
   key : 'id',
   values : '123',
});
navigator.sendBeacon(url, params);

multipart/form-data
To send multipart/form-data header, use FormData instance.

// 2. FormData String
const params = new FormData();
params.append('key', 'id');
params.append('value', '123');
navigator.sendBeacon(url, params);

application/json
To send application/json header, use Blob and set its type.

// 3. JSON Object
const data = {
   key : 'id',
   values : '123',
};

const params = new Blob(
    [JSON.stringify(data)], 
    {type : 'application/json'}
);

navigator.sendBeacon(url, params);


refs

https://www.cnblogs.com/xgqfrms/p/14634832.html



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(121)  评论(11编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-04-09 calendar merge date
2020-04-09 js 获取是否为闰年,以及各月的天数 & isLeapYear
2020-04-09 scrollTo & js auto scroll & scrollX & scrollY
2019-04-09 email 聚合 & 批量删除
2019-04-09 windows 10 & 禁用服务.bat
2019-04-09 vue render img
2016-04-09 Essential Sublime Text 3 Plugins for PHP Developers, Sublime Text (3) for PHP Developers
点击右上角即可分享
微信分享提示