test-params.html 4.38 KB
<!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>