【问题标题】:Setting response header for Content-Type as "application/json; charset=utf-16" converts json to chinese character将 Content-Type 的响应头设置为 "application/json; charset=utf-16" 将 json 转换为中文字符
【发布时间】:2019-12-19 17:23:43
【问题描述】:

当我尝试在我的服务中设置响应标头时,它会将 json 转换为中文字符和其他一些垃圾字符。自 2 天以来,我一直在尝试调试此问题。我正在使用快递服务器。

【问题讨论】:

    标签: node.js express utf-16


    【解决方案1】:

    我创建了一个简单的测试服务器,它向客户端发送基本的 json 响应,我没有看到出现中文字符的问题。我还测试了更大的 json 响应。

    您可以试一试,看看是否得到相同的结果。

    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/json', (req, res) => {
        res.set({ 'content-type': 'application/json; charset=utf-16' });
        res.write( JSON.stringify( { foo: 'bar' } ) )
        res.end();
    })
    
    app.listen(port, () => console.log(`Express listening on port ${port}!`))
    

    【讨论】:

    • 还是同样的问题,我在postman上试过了,你用的是哪个版本的node?​​span>
    • 我正在使用 Node v10.15.1、Windows 10、Express 4.17.1,嗯.. 我想知道是什么导致了差异.. 我使用 curl 进行了测试,我会尝试 postman..跨度>
    • 我认为这是一个与操作系统相关的问题,我的服务器在 aws 上的 ubuntu 中运行,我在 mac 上进行本地测试
    • 有可能!我已经使用邮递员进行了测试,虽然我没有看到中文字符,但在使用 utf-16 字符编码时由于某种原因我得到了一个空响应。 Curl 似乎并不关心编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多