我这辈子都想不通。下面是请求模块的实现,但我也尝试使用node-XMLHttpRequest模块无济于事。
var request = require('request');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request.get({ url: url }, function(error, response, body) {
if (error || response.statusCode !== 200) {
console.log('There was a problem with the request');
return;
}
console.log(body); // outputs gibberish characters like �
console.log(body.toString()); // also outputs gibberish
});
似乎是一个编码问题,但我在浏览器中使用了完全相同的代码(带有本机 XHR 对象)并且它可以正常工作。我究竟做错了什么?