使用本機「http」模塊的HTTP請求的響應正文,顯示unicode字符的問號字符,而不是其實際值。這是我正在運行的代碼的基本代碼片段。Node.js與HTTP響應正文unicode問題
var http = require('http');
var google = http.createClient(80, 'www.google.it');
var request = google.request('GET', '/',
{
'host': 'www.google.it',
}
);
request.end();
request.on('response', function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log(chunk);
});
});
在響應中有一個以「Pubblicit」開頭的特定詞。它的最後一個字母是一個奇怪的字符,顯示爲一個問號給我。這個詞應該是Pubblicità,而不是Pubblicit?。
我也曾嘗試使用.toString()
輸出數據:
console.log(chunk.toString());
或
console.log(chunk.toString('utf8'));
但我得到了相同的結果。
有什麼想法?
什麼是您的操作系統? –
嘗試檢查'chunk.charCodeAt(chunk.length - 1)'。如果它是'224'('à'),問題出在你的控制檯/區域設置上。 – katspaugh
@PabloFernandez,使用Mac OS X雪豹 –