form-data-test.html
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Data Test</title>
<script src="../dist/index.umd.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.container {
display: flex;
gap: 20px;
}
.panel {
flex: 1;
border: 1px solid #ccc;
border-radius: 5px;
padding: 15px;
}
button {
margin: 10px 0;
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
pre {
background-color: #f5f5f5;
padding: 10px;
border-radius: 5px;
overflow: auto;
max-height: 300px;
}
</style>
</head>
<body>
<h1>POST请求参数测试</h1>
<div class="container">
<div class="panel">
<h2>JSON格式</h2>
<p>默认使用JSON格式发送POST请求</p>
<button id="testJsonBtn">测试JSON格式</button>
<pre id="jsonResult">结果将显示在这里...</pre>
</div>
<div class="panel">
<h2>表单格式</h2>
<p>使用application/x-www-form-urlencoded格式发送POST请求</p>
<button id="testFormBtn">测试表单格式</button>
<pre id="formResult">结果将显示在这里...</pre>
</div>
</div>
<script>
// 初始化请求库
const GlskFetch = window.GlskFetch.default;
// 创建请求实例,提供必要的初始化参数
const fetch = new GlskFetch({
sentryDsn: 'https://example@sentry.io/123456', // 提供一个示例Sentry DSN
disableSentry: true // 禁用Sentry,避免实际发送错误报告
});
// 测试数据
const testData = {
name: '测试用户',
age: 30,
items: ['item1', 'item2']
};
// 测试JSON格式
document.getElementById('testJsonBtn').addEventListener('click', async () => {
try {
const response = await fetch.post('https://httpbin.org/post', testData);
document.getElementById('jsonResult').textContent = JSON.stringify(response.data, null, 2);
console.log('JSON格式响应:', response);
} catch (error) {
document.getElementById('jsonResult').textContent = '错误: ' + error.message;
console.error('JSON格式错误:', error);
}
});
// 测试表单格式
document.getElementById('testFormBtn').addEventListener('click', async () => {
try {
const response = await fetch.post('https://httpbin.org/post', testData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
document.getElementById('formResult').textContent = JSON.stringify(response.data, null, 2);
console.log('表单格式响应:', response);
} catch (error) {
document.getElementById('formResult').textContent = '错误: ' + error.message;
console.error('表单格式错误:', error);
}
});
</script>
</body>
</html>