NodeJs发出一个API重音错误的请求

我有一个问题,当我张贴JSON的口音(如éè…)服务器发回400错误的请求错误。 我不知道如何解决这个问题。

bodyStr ='{"name":"ééé"}'; //headers var headers = { 'Content-Type': 'application/json', 'Content-Length': bodyStr.length, 'X-Key' : this.xkey }; //http request request({ uri : this.httpHost + path, method: method, headers : headers, body : bodyStr }, function (error, response, body) { if (!error && response.statusCode < 400) { if (typeof successCallback == "function") { successCallback(JSON.parse(body), response); } } else { if (typeof errorCallback == "function") { errorCallback(error || response.body || response.statusCode, response); } } } ); 

您是否实施了RESTful服务? 在回应内容中,您是否有其他提示?

也许在头Content-type设置字符集(latin1)可以解决这个问题:

 Content-Type: application/json; charset=iso-8859-1 

希望它能帮助你,Thierry

为了解决这个问题,你需要用像iconv-lite( http://github.com/ashtuchkin/iconv-lite )这样的模块将string转换为utf-8:

 var iconv = require('iconv-lite'); bodyStr = iconv.encode(bodyStr, 'utf-8');