【发布时间】:2019-08-11 08:39:52
【问题描述】:
我用 Node 和 Express 构建了一个 API,它返回一些 JSON。 JSON 数据将由 Web 应用程序读取。遗憾的是,这个应用程序只接受 ISO-8859-1 编码的 JSON,这已被证明有点困难。
即使我尝试了 Express 文档中的方法以及谷歌搜索问题的所有提示,我也无法设法返回具有正确编码的 JSON。
Express 文档说要使用“res.set()”或“res.type()”,但这些都不适合我。注释行是我尝试过的所有变体(使用 Mongoose):
MyModel.find()
.sort([['name', 'ascending']])
.exec((err, result) => {
if (err) { return next(err) }
// res.set('Content-Type', 'application/json; charset=iso-8859-1')
// res.set('Content-Type', 'application/json; charset=ansi')
// res.set('Content-Type', 'application/json; charset=windows-1252')
// res.type('application/json; charset=iso-8859-1')
// res.type('application/json; charset=ansi')
// res.type('application/json; charset=windows-1252')
// res.send(result)
res.json(result)
})
这些都对响应没有任何影响,它总是变成“Content-Type: application/json; charset=utf-8”。
既然 JSON 应该(?)以 utf-8 编码,是否可以在 Express 中使用任何其他编码?
【问题讨论】:
标签: node.js json express utf-8 iso-8859-1