test-params.html
4.38 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GET请求参数测试</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.result {
margin-top: 10px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
background-color: #f9f9f9;
white-space: pre-wrap;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>GET请求参数测试</h1>
<div>
<h2>普通参数测试</h2>
<button id="normalParamsTest">测试普通参数</button>
<div id="normalParamsResult" class="result"></div>
</div>
<div>
<h2>数组参数测试</h2>
<button id="arrayParamsTest">测试数组参数</button>
<div id="arrayParamsResult" class="result"></div>
</div>
<script src="./dist/index.umd.js"></script>
<script>
// 创建请求实例
const request = new Request({
baseURL: 'https://httpbin.org',
timeout: 5000,
appId: 'test_app_id',
appSecret: 'test_app_secret'
});
// 显示结果函数
function showResult(elementId, data) {
const resultElement = document.getElementById(elementId);
resultElement.textContent = JSON.stringify(data, null, 2);
}
// 测试普通参数
document.getElementById('normalParamsTest').addEventListener('click', () => {
const resultElement = document.getElementById('normalParamsResult');
resultElement.textContent = '请求中...';
console.log('发送普通参数请求');
request.get('/get', {
params: {
name: '张三',
age: 25,
active: true
}
})
.then(response => {
console.log('普通参数请求响应:', response);
console.log('请求URL:', response.config.url);
console.log('请求参数:', response.config.params);
console.log('响应参数:', response.data.args);
showResult('normalParamsResult', {
url: response.config.url, // 显示完整URL
params: response.config.params, // 显示参数对象
args: response.data.args // httpbin返回的解析后的参数
});
})
.catch(error => {
console.error('普通参数请求失败:', error);
resultElement.textContent = `请求失败: ${error.message}`;
});
});
// 测试数组参数
document.getElementById('arrayParamsTest').addEventListener('click', () => {
const resultElement = document.getElementById('arrayParamsResult');
resultElement.textContent = '请求中...';
console.log('发送数组参数请求');
request.get('/get', {
params: {
id: [1, 2, 3],
tags: ['javascript', 'typescript', 'fetch']
}
})
.then(response => {
console.log('数组参数请求响应:', response);
console.log('请求URL:', response.config.url);
console.log('请求参数:', response.config.params);
console.log('响应参数:', response.data.args);
showResult('arrayParamsResult', {
url: response.config.url, // 显示完整URL
params: response.config.params, // 显示参数对象
args: response.data.args // httpbin返回的解析后的参数
});
})
.catch(error => {
console.error('数组参数请求失败:', error);
resultElement.textContent = `请求失败: ${error.message}`;
});
});
</script>
</body>
</html>