Axios返回奇怪的垃圾值,例如 �2���ϫ��M@�~�II�{!

我使用的是WSL2 ubuntu, node 版本是v19.0,axios是1.2.0

我的代码再简单不过了: `

const axios = require("axios");

axios.get('https://jsonplaceholder.typicode.com/users')
    .then(response => console.log(response.data))

FETCH()非常有效.有什么 idea 或可能的解决方案吗?

推荐答案

需要在axios.get头中加上Accept-Encodingapplication/json.

默认为gzip

这是演示代码

const axios = require("axios");

const getUsers = async () => {
    try {
        const resp = await axios.get('https://jsonplaceholder.typicode.com/users',
            {
                headers: {
                    'Accept-Encoding': 'application/json',
                }
            }
        );
        console.log(JSON.stringify(resp.data, null, 4));
    } catch (err) {
        // Handle Error Here
        console.error(err);
    }
};

getUsers();

或者你的代码风格.

const axios = require("axios");

axios.get('https://jsonplaceholder.typicode.com/users',
    {
        headers: {
            'Accept-Encoding': 'application/json',
        }
    })
    .then(response => console.log(response.data))

结果

$ node test.js
[
    {
        "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "Sincere@april.biz",
        "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
            }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
            "name": "Romaguera-Crona",
            "catchPhrase": "Multi-layered client-server neural-net",
            "bs": "harness real-time e-markets"
        }
    },
... removed

Javascript相关问答推荐

更改JSON中使用AJAX返回的图像的路径

简单的PayPal按钮集成导致404错误

使用Java脚本导入gltf场景并创建边界框

更改预请求脚本中重用的JSON主体变量- Postman

对路由DOM嵌套路由作出react

React.Development.js和未捕获的ReferenceError:未定义useState

查询参数未在我的Next.js应用路由接口中定义

元素字符串长度html

使用类型:assets资源 /资源&时,webpack的配置对象&无效

在渲染turbo流之后滚动到元素

在SuperBase JS客户端中寻址JSON数据

重新呈现-react -筛选数据过多

JAVASCRIPT SWITCH CASE语句:当表达式为';ALL';

在范围数组中查找公共(包含)范围

REACT-本机错误:错误类型错误:无法读取未定义的容器的属性

我如何让我的弹力球在JavaScript和HTML画布中相互碰撞后改变 colored颜色 ?

Html/JS:如何在脚本执行前阻止呈现?

VITE版本中的内联SVG

如何在拆分按钮Primeng 17中更改图标下拉菜单

素材界面如何让自动完成弹出框不受菜单限制