Skip to content

Commit 211d853

Browse files
committed
update
1 parent 5e84831 commit 211d853

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

NET网络请求/Fetch API/Navigator.sendBeacon()用法示例.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ <h3>与传统方法的比较</h3>
303303
});
304304

305305
// 使用 sendBeacon 发送数据
306-
const success = navigator.sendBeacon('https://httpbin.org/post', data);
306+
const success = navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
307307

308308
if (success) {
309309
addLog('sendBeacon 请求已成功加入发送队列', 'success');
@@ -325,7 +325,7 @@ <h3>与传统方法的比较</h3>
325325
testId: 'unload_test_' + Date.now()
326326
});
327327

328-
navigator.sendBeacon('https://httpbin.org/post', data);
328+
navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
329329
});
330330

331331
// 3秒后跳转
@@ -353,7 +353,7 @@ <h3>与传统方法的比较</h3>
353353
timeSpent: Math.round(performance.now())
354354
});
355355

356-
navigator.sendBeacon('https://httpbin.org/post', data);
356+
navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
357357
});
358358

359359
// 添加一些模拟分析事件
@@ -367,7 +367,7 @@ <h3>与传统方法的比较</h3>
367367
});
368368

369369
// 使用 sendBeacon 发送点击数据
370-
navigator.sendBeacon('https://httpbin.org/post', data);
370+
navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
371371
});
372372
</script>
373373
</body>

NET网络请求/Fetch API/Navigator.sendBeacon()请求方式详解.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ <h3>代码示例</h3>
404404
// 测试发送字符串数据
405405
testStringBtn.addEventListener('click', function() {
406406
const data = `event=button_click&id=testString&time=${Date.now()}&random=${Math.random()}`;
407-
const success = navigator.sendBeacon('https://httpbin.org/post', data);
407+
const success = navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
408408

409409
if (success) {
410410
addLog('字符串数据已加入发送队列', 'success');
@@ -420,7 +420,7 @@ <h3>代码示例</h3>
420420
formData.append('timestamp', Date.now().toString());
421421
formData.append('userAgent', navigator.userAgent);
422422

423-
const success = navigator.sendBeacon('https://httpbin.org/post', formData);
423+
const success = navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', formData);
424424

425425
if (success) {
426426
addLog('FormData 已加入发送队列', 'success');
@@ -439,7 +439,7 @@ <h3>代码示例</h3>
439439
};
440440

441441
const blob = new Blob([JSON.stringify(jsonData)], { type: 'application/json' });
442-
const success = navigator.sendBeacon('https://httpbin.org/post', blob);
442+
const success = navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', blob);
443443

444444
if (success) {
445445
addLog('Blob 数据已加入发送队列', 'success');
@@ -456,7 +456,7 @@ <h3>代码示例</h3>
456456
view[0] = 123456;
457457
view[1] = 789012;
458458

459-
const success = navigator.sendBeacon('https://httpbin.org/post', buffer);
459+
const success = navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', buffer);
460460

461461
if (success) {
462462
addLog('ArrayBuffer 数据已加入发送队列', 'success');
@@ -481,7 +481,7 @@ <h3>代码示例</h3>
481481
referrer: document.referrer
482482
});
483483

484-
navigator.sendBeacon('https://httpbin.org/post', data);
484+
navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
485485
addLog('页面卸载事件已触发,sendBeacon 已调用', 'info');
486486
});
487487

@@ -511,7 +511,7 @@ <h3>代码示例</h3>
511511
timeSpent: Math.round(performance.now())
512512
});
513513

514-
navigator.sendBeacon('https://httpbin.org/post', data);
514+
navigator.sendBeacon('http://127.0.0.1:666/api/v1/insert', data);
515515
});
516516
</script>
517517
</body>

NET网络请求/Fetch API/koa-server/app.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ App.use(
2626
},
2727
maxAge: 5, //指定本次预检请求的有效期,单位为秒。
2828
credentials: true, //是否允许发送Cookie
29-
// allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], //设置所允许的HTTP请求方法
29+
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], //设置所允许的HTTP请求方法
3030
// allowHeaders: ["Content-Type", "Authorization", "Accept"], //设置服务器支持的所有头信息字段
3131
// exposeHeaders: ["WWW-Authenticate", "Server-Authorization"], //设置获取其他自定义字段
3232
})
3333
);
3434

35-
Router.get("/", (ctx, next) => {
35+
// 默认get路由
36+
Router.get("/", async (ctx, next) => {
3637
ctx.body = "欢迎使用 Koa Server!";
38+
await next();
3739
})
38-
.get("/random.js", (ctx, next) => {
39-
var arr = [
40+
41+
// get
42+
.get("/random.js", async (ctx, next) => {
43+
const arr = [
4044
"http://www.baidu.com",
4145
2,
4246
"https://www.2345.com/?kmupiao",
@@ -60,13 +64,16 @@ Router.get("/", (ctx, next) => {
6064
})
6165

6266
// post
63-
.post("/post-logs", async (ctx, next) => {
67+
.post("/insert", async (ctx, next) => {
6468
console.log(ctx.request);
65-
console.log(ctx.request.body);
69+
console.log('query:', ctx.query);
70+
console.log('body:', ctx.request.body);
71+
6672
ctx.body = {
6773
code: 200,
6874
data: {
6975
list: [],
76+
res: ctx.request.body,
7077
},
7178
message: "post ok!",
7279
timestamp: Date.now(),
@@ -76,7 +83,8 @@ Router.get("/", (ctx, next) => {
7683
// put
7784
.put("/update", async (ctx, next) => {
7885
console.log(ctx.request);
79-
console.log(ctx.request.body);
86+
console.log('query:', ctx.query);
87+
console.log('body:', ctx.request.body);
8088

8189
ctx.body = {
8290
code: 200,

NET网络请求/Fetch API/如何在关闭浏览器标签前,可靠地发送 HTTP 请求.html

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <h3>解决方案一:navigator.sendBeacon()</h3>
4343
可靠性高: 由浏览器保证发送,不受页面卸载影响。
4444
异步非阻塞:不影响用户关闭页面的速度和体验。
4545
使用简单:API 非常直观。
46-
数据有限制:只能单向发送 POST 请求【只支持POST请求】,且无法自定义请求头(Headers)
46+
数据有限制:只能单向(不能处理响应)的发送 POST 请求【只能POST请求】,且无法自定义请求头(Headers)
4747

4848
<h3>解决方案二:fetch() 与 keepalive: true</h3>
4949
fetch API 作为现代网络请求的基石,也提供了一种优雅的解决方案。
@@ -71,23 +71,32 @@ <h3>解决方案二:fetch() 与 keepalive: true</h3>
7171
{
7272
function sendBeaconData() {
7373
// 要发送的数据,内容格式都可自定义
74-
const data1 = {
74+
const data1 = JSON.stringify({
7575
lastAction: "close_tab",
7676
content: "我是要发送的数据",
77+
url: window.location.href,
78+
timestamp: Date.now(),
7779
timeOnPage: Math.round(performance.now()),
78-
};
80+
});
7981

8082
// 将数据转换为 Blob类型(这是 sendBeacon 支持的格式之一)
81-
const blob = new Blob([JSON.stringify(data1)], {
83+
const body = new Blob([data1], {
8284
type: "application/json; charset=UTF-8",
8385
});
8486

87+
// 创建一个 URLSearchParams 对象,将数据转换为查询字符串
88+
const params = new URLSearchParams();
89+
params.append("lastAction", "close_tab");
90+
params.append("content", "我是要发送的数据");
91+
params.append("timestamp", Date.now());
92+
8593
// 使用 sendBeacon 发送数据
8694
// 该方法会返回 true (成功加入队列) 或 false (数据过大或格式错误)
8795
const success = navigator.sendBeacon(
88-
"https://www.example.com/api/v1/post-logs",
89-
blob
96+
"http://127.0.0.1:666/api/v1/insert?" + params.toString(),
97+
body
9098
);
99+
91100
if (success) {
92101
console.log("OK,要发送的数据已成功加入发送队列!");
93102
} else {
@@ -142,7 +151,7 @@ <h3>解决方案二:fetch() 与 keepalive: true</h3>
142151
// 在这种情况下,我们通常不发送信标
143152
return;
144153
}
145-
fetchSendData();
154+
// fetchSendData();
146155
});
147156

148157
document.querySelector("#fetch-btn").onclick = function () {

0 commit comments

Comments
 (0)