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>
相关推荐
延晗5 分钟前
【从零开始 DeepSeek Harness 插件开发教程】三、从零开发 DeepSeek Harness 插件——完成第一个插件的挂载与运行
前端·人工智能
择势24 分钟前
iOS 工程环境绝对隔离实践:开发机的"编不出"生产包
前端
某哈压力大29 分钟前
Vue的双向绑定与自定义组件的v-model
前端·面试
CappuccinoRose1 小时前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
天天鸭1 小时前
前端转全栈,要学会后端看项目的思维
前端·后端·ai编程
oil欧哟1 小时前
AI Agent 常用操作入门:独立目录、Plan 模式与跨话题交接指南
前端
孟陬1 小时前
Foobar 技术周刊 #161:AI 录屏员工屏幕训练 SOP,被当做「动物园的猴子」是否触犯隐私?
前端·后端·程序员
1-1=02 小时前
Vue 混入(Mixin)和 2.插件 (Plugins)
前端·vue.js
用户298698530142 小时前
React 前端实现 Excel 与 OpenXML 双向转换:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel
-梅2 小时前
linux(番外)线程,锁,条件变量封装;日志与线程池
linux·c语言·前端·c++