fetch请求代码

用于方便与后端做简单的接口测试(参数编码与不编码)

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fetch Request</title>
</head>

<body>
    <button id="fetchButton">Fetch Data</button>
    <div id="responseContainer"></div>

    <script>
        // 定义 URL 和参数
        const baseUrl = 'http://10.30.20.146:20510/api/v1/text/page';
        const params = {
            // textName: '!@中国# $%^&/*\=()_+??><[]{}',
            textName: "!@中国人$%^_+??><`~!@#$%^&*()_-+=|\}{;:'/?.>,</*-+.[",
            array: [1, 2, 3],
            page: 1,
            pageSize: 20,
            domainType: 1,
            decode: 1
        };

        // 获取按钮元素
        const fetchButton = document.getElementById('fetchButton');
        fetchButton.addEventListener('click', () => {
            performRequest(baseUrl, params, false);
        });

        async function performRequest(url, params, encodeParams = true) {
            // 构建完整的 URL
            const urlWithParams = buildUrlWithParams(url, params, encodeParams);
            console.log('===[[urlWithParams]]', urlWithParams);

            // 请求选项
            const options = {
                method: 'GET',
                headers: {
                    'Accept': '*/*',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Connection': 'keep-alive',
                    'User-Agent': 'PostmanRuntime-ApipostRuntime/1.1.0',
                    '_appId': '1',
                    '_tenantId': '1',
                    '_userId': '3026601398293760000'
                }
            };

            try {
                const response = await fetch(urlWithParams, options);
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                const data = await response.json();
                console.log('Response data:', data);
                displayResponse(data);
            } catch (error) {
                console.error('Fetch error:', error);
            }
        }

        function buildUrlWithParams(url, params, encodeParams) {
            const urlObj = new URL(url);
            for (const [key, value] of Object.entries(params)) {
                if (encodeParams) {
                    urlObj.searchParams.append(key, encodeURIComponent(value));
                } else {
                    urlObj.searchParams.append(key, value);
                }
            }
            return urlObj.toString();
        }

        function displayResponse(data) {
            const responseContainer = document.getElementById('responseContainer');
            responseContainer.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
        }
    </script>
</body>

</html>
相关推荐
Mike_jia7 分钟前
NginxPulse:Nginx日志监控革命!实时洞察Web流量与安全态势的智能利器
前端
风之舞_yjf12 分钟前
Vue基础(31)_插件(plugins)、scoped样式
前端·vue.js
M ? A20 分钟前
Vue3+TS实战避坑指南
前端·vue.js·经验分享
Mintopia26 分钟前
你以为是技术问题,其实是流程问题:工程效率的真相
前端
Mintopia32 分钟前
一套能落地的"防 Bug"习惯:不用加班也能少出错
前端
亿元程序员34 分钟前
箭头游戏那么火,搞个3D的可以吗?我:这不是3年前的游戏了吗?
前端
IT_陈寒35 分钟前
SpringBoot里的这个坑差点让我加班到天亮
前端·人工智能·后端
巫山老妖39 分钟前
大模型工程三驾马车:Prompt Engineering、Context Engineering 与 Harness Engineering 深度解析
前端
Cobyte43 分钟前
4.响应式系统基础:从发布订阅模式的角度理解 Vue3 的数据响应式原理
前端·javascript·vue.js
晓得迷路了1 小时前
栗子前端技术周刊第 124 期 - ESLint v10.2.0、React Native 0.85、Node.js 25.9.0...
前端·javascript·eslint