关于nodejs使用axios发送请求返回中文乱码问题

Last updated on January 10, 2023 am

问题描述

在浏览器直接请求太平洋的ip查询接口返回结果正常,但是在nodejs中返回的中文结果为乱码。

解决方法

下载iconv-lite模块,对接受结果进行转码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const iconv = require("iconv-lite")

axios({
method: 'get',
url: "http://whois.pconline.com.cn/ipJson.jsp?ip=6.6.3.44&json=true",
responseType: "arraybuffer" //添加配置
}).then(ret => {
//以下
var cityInfo = iconv.decode(ret.data, 'gbk');
cityInfo=JSON.parse(cityInfo)
//以上
res.json({
code: 200,
city:cityInfo.addr
})
})

关于nodejs使用axios发送请求返回中文乱码问题
https://angelaggression.github.io/2022/10/13/关于nodejs使用axios发送请求返回中文乱码问题/
Author
BurnedWings
Posted on
October 13, 2022
Licensed under