Phi*_*hil 3 javascript node.js fetch-api axios
Axios 返回奇怪的垃圾值,例如\n\xef\xbf\xbd2\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xcf\xab\xef\xbf\xbd\xef\xbf\xbdM@\xef\xbf\xbd~\xef\xbf\xbdII\xef\xbf\xbd{!
我在 wsl2 ubuntu 上,节点版本是 v19.0,axios 是 1.2.0
\n我的代码再简单不过了:\n`
\nconst axios = require("axios");\n\naxios.get('https://jsonplaceholder.typicode.com/users')\n .then(response => console.log(response.data))\nRun Code Online (Sandbox Code Playgroud)\nfetch() 工作起来就像一个魅力。有什么想法或可能的解决方案吗?
\n您需要在 v1.2.0 中的 axios.get 标头中添加Accept-Encodingwithapplication/json
它的默认值是gzip
这是v1.2.0中的演示代码
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();
Run Code Online (Sandbox Code Playgroud)
或者你的代码风格。
const axios = require("axios");
axios.get('https://jsonplaceholder.typicode.com/users',
{
headers: {
'Accept-Encoding': 'application/json',
}
})
.then(response => console.log(response.data))
Run Code Online (Sandbox Code Playgroud)
结果
$ 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
598 次 |
| 最近记录: |