如何在node.js中将数据转换为utf-8?
我正在使用带有express的node.js。我使用 Mongoose 从 MongoDB 读取数据,并使用 res.send(data)
以正常方式传送数据。不幸的是,某些请求的交付失败。即使标头显示编码是 utf-8,但在某些情况下似乎是 ANSI,导致 jsonp 回调函数失败并出现错误。
您可以在此页面重现错误:http://like-my-style.com/#!single/9837034 。 jsonp 调用仅在某些产品上失败,其中大多数(以及具有特殊字符的产品)工作正常。
如何确保给定的字符串在 node.js 中以 utf-8 编码?
I am using node.js with express. I read out data from MongoDB with Mongoose and deliver it the normal way with res.send(data)
. Unfortunately the delivering fails for some requests. Even so the header says the encoding is utf-8, it seems to be ANSI in some cases, causing the jsonp callback function to fail with an error.
You can reproduce the error at this page: http://like-my-style.com/#!single/9837034 . The jsonp call fails just on some products, most of them (also the ones with special chars) work fine.
How can I ensure, that a given String is encoded in utf-8 in node.js?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有没有尝试过:
确保你的数据是utf8而不是Buffer。
Have you tried:
To ensure that your data is in utf8 and is not Buffer.
我想我陷入了类似的问题,neebz 解决方案有效,但我必须把它放在正确的位置。
在 node.js 文档中,其记录为 request.setEncoding() ,这可能是一个错误,因为需要在请求创建的 res 对象上调用它。
I think I got stuck in a similar issue and neebz solution worked but I had to put it in the right spot.
In the node.js docs its documented as request.setEncoding() which might be an error because it needs to be called on the res object that is created by the request.
您是否设置编码类型
res.setEncoding('utf8');
?
Are you setting the encoding type
res.setEncoding('utf8');
?