我检查了stackoverflow中的所有解决方案,但他们没有与我合作,我无法理解发生了什么。
这是我的代码:
var data = "";
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function () {
// Removed console.log() and after removing it didn't work so Please only answer my question and don't talk about silly things like a missing bracket -_!
//The error is here
var json = JSON.parse(data.toString());
switch (json.method) {
...
}
});
这里的控制台输出:
答案 0 :(得分:0)
看起来您的编码未设置...尝试使用res.setEncoding('utf8');
答案 1 :(得分:0)
typeof(data)
返回什么?
如果字符串不使用toString()
如果它已经是一个对象,toString()
会使它失败。此外,您不需要使用JSON.parse()