// be warned that I don't know axios, I assume this is possible but it's
// not the right syntax, i just made it up.
const notificationsBuffer = await axios.get(url, {return: 'buffer'});
// Once you have the buffer, this line _should_ be correct.
const notifications = JSON.parse(notificationBuffer.toString('ISO-8859-1'));
2条答案
按热度按时间p8h8hvxi1#
text/json; charset=iso-8859-1
不是有效的标准内容类型。text/json
错误,JSON必须是UTF-8。因此,至少在服务器上解决这个问题的最好方法是首先获得一个缓冲区(axios支持返回缓冲区吗?),将其转换为UTF-8字符串(唯一的法律的JavaScript字符串),然后才对其运行
JSON.parse
。伪代码:
gdrx4gfi2#
接受的答案对我不起作用,但这条有用的评论:
如果你的响应是JSON,那么你可以用
JSON.parse(data.toString("latin1"))
解析它。请参阅
.toString()
支持的this list of encodings。然而,Node.js特有的一种更简单的方法是设置
responseEncoding
request configuration键:由于
fetch
现在在Node 18+和浏览器中都是标准的,这里有一个例子(错误处理和JSON.parse()
省略):