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>
相关推荐
JinSo4 小时前
我的2025年度总结:EasyEditor
前端·程序员
喝拿铁写前端8 小时前
前端开发者使用 AI 的能力层级——从表面使用到工程化能力的真正分水岭
前端·人工智能·程序员
wuhen_n9 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
七月shi人9 小时前
AI浪潮下,前端路在何方
前端·人工智能·ai编程
非凡ghost9 小时前
MusicPlayer2(本地音乐播放器)
前端·windows·学习·软件需求
脾气有点小暴9 小时前
scroll-view分页加载
前端·javascript·uni-app
beckyye10 小时前
ant design vue Table根据数据合并单元格
前端·antd
布列瑟农的星空10 小时前
还在手动翻译国际化词条?AST解析+AI翻译实现一键替换
前端·后端·ai编程
土豆125010 小时前
Rust 错误处理完全指南:从入门到精通
前端·rust·编程语言