simple-get-test.html
6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>simpleGet函数测试</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.test-section {
background: white;
margin: 20px 0;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.test-section h3 {
color: #333;
margin-top: 0;
}
button {
background: #007cba;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
margin: 5px;
}
button:hover {
background: #005a87;
}
.result {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
background: #f8f9fa;
border-left: 4px solid #007cba;
max-height: 300px;
overflow-y: auto;
}
.error {
border-left-color: #dc3545;
background: #f8d7da;
}
.success {
border-left-color: #28a745;
background: #d4edda;
}
pre {
white-space: pre-wrap;
word-break: break-all;
}
</style>
</head>
<body>
<h1>simpleGet函数测试</h1>
<p>测试从主入口导出的simpleGet函数,用于简单的GET请求</p>
<!-- 测试1:基本GET请求 -->
<div class="test-section">
<h3>测试1:基本GET请求</h3>
<p>使用simpleGet函数发送简单的GET请求</p>
<button onclick="testBasicGet()">测试基本GET请求</button>
<div id="result1" class="result" style="display: none;"></div>
</div>
<!-- 测试2:JSON API请求 -->
<div class="test-section">
<h3>测试2:JSON API请求</h3>
<p>请求JSON格式的API数据</p>
<button onclick="testJsonApi()">测试JSON API</button>
<div id="result2" class="result" style="display: none;"></div>
</div>
<!-- 测试3:错误处理 -->
<div class="test-section">
<h3>测试3:错误处理</h3>
<p>测试无效URL的错误处理</p>
<button onclick="testErrorHandling()">测试错误处理</button>
<div id="result3" class="result" style="display: none;"></div>
</div>
<script type="module">
// 导入 simpleGet 函数
import { simpleGet } from '../dist/index.esm.js';
// 检查函数是否正确导出
if (typeof simpleGet !== 'function') {
console.error('❌ simpleGet函数未正确导出');
alert('❌ simpleGet函数未正确导出');
throw new Error('simpleGet函数未正确导出');
}
console.log('✅ simpleGet函数导出成功,类型:', typeof simpleGet);
// 将函数挂载到全局对象以供其他脚本使用
window.simpleGet = simpleGet;
// 测试1:基本GET请求
window.testBasicGet = async function() {
const resultDiv = document.getElementById('result1');
resultDiv.style.display = 'block';
resultDiv.className = 'result';
resultDiv.innerHTML = '请求中...';
try {
if (!simpleGet) {
throw new Error('simpleGet函数未正确导出');
}
// 测试一个简单的API
const response = await simpleGet('https://httpbin.org/get');
resultDiv.className = 'result success';
resultDiv.innerHTML = `
<strong>✓ 请求成功</strong><br>
<strong>响应数据:</strong><br>
<pre>${JSON.stringify(response, null, 2)}</pre>
`;
} catch (error) {
resultDiv.className = 'result error';
resultDiv.innerHTML = `
<strong>✗ 请求失败</strong><br>
错误信息:${error.message}<br>
<pre>${error.stack || ''}</pre>
`;
}
}
// 测试2:JSON API请求
window.testJsonApi = async function() {
const resultDiv = document.getElementById('result2');
resultDiv.style.display = 'block';
resultDiv.className = 'result';
resultDiv.innerHTML = '请求中...';
try {
if (!simpleGet) {
throw new Error('simpleGet函数未正确导出');
}
// 测试JSON API
const response = await simpleGet('https://jsonplaceholder.typicode.com/posts/1');
resultDiv.className = 'result success';
resultDiv.innerHTML = `
<strong>✓ JSON API请求成功</strong><br>
<strong>文章标题:</strong>${response.title}<br>
<strong>文章内容:</strong>${response.body}<br>
<strong>完整响应:</strong><br>
<pre>${JSON.stringify(response, null, 2)}</pre>
`;
} catch (error) {
resultDiv.className = 'result error';
resultDiv.innerHTML = `
<strong>✗ JSON API请求失败</strong><br>
错误信息:${error.message}<br>
<pre>${error.stack || ''}</pre>
`;
}
}
// 测试3:错误处理
window.testErrorHandling = async function() {
const resultDiv = document.getElementById('result3');
resultDiv.style.display = 'block';
resultDiv.className = 'result';
resultDiv.innerHTML = '测试中...';
try {
if (!simpleGet) {
throw new Error('simpleGet函数未正确导出');
}
// 测试无效URL
await simpleGet('');
resultDiv.className = 'result error';
resultDiv.innerHTML = '<strong>✗ 错误处理测试失败</strong><br>应该抛出错误但没有';
} catch (error) {
resultDiv.className = 'result success';
resultDiv.innerHTML = `
<strong>✓ 错误处理正常</strong><br>
正确捕获到错误:${error.message}
`;
}
}
// 页面加载时检查函数是否可用
window.addEventListener('load', function() {
if (simpleGet) {
console.log('✓ simpleGet函数已成功导出并可用');
} else {
console.error('✗ simpleGet函数未正确导出');
}
});
</script>
</body>
</html>