我無法弄清楚這是否適合我的生活。以下是request模塊的實現,但我也嘗試使用node-XMLHttpRequest模塊無濟於事。從StackOverflow API請求JSON時的編碼問題
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對象)和它的作品沒有任何問題。我究竟做錯了什麼?
是的,這是有道理的。謝謝! – 2013-02-10 01:46:33