我正在使用 node.js 并尝试解析请求的 JSON 正文。我收到以下错误:
undefined:0
^
SyntaxError: Unexpected end of input
at Object.parse (native)
at IncomingMessage.<anonymous> (C:\node\xxxx.js:36:14)
at IncomingMessage.emit (events.js:64:17)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:130:23)
at Socket.ondata (http.js:1506:22)
at TCP.onread (net.js:374:27)
我在做:
request.on('data', function(chunk)
{
data+=chunk;
});
// and in the end I am doing
obj = JSON.parse(data); // it's complaining at this point.
输入是:
{
"result": "success",
"source": "chat"
}