form-data-test.html 3.62 KB
<!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>