我正在执行对返回简单JSON对象的url的get请求。Node.js中的JSON.parse问题
这里是网址:http://live.albiononline.com/status.txt
正如你可以看到它是一个文本文件,它的内容类型标题为text/plain。当我收到的身体日志,它是:
{ "status": "online", "message": "All good." }
但是当我尝试登录,它body.status是不确定的。当我做var data = JSON.parse(body);
然后console.log(data.status);
我得到:
undefined:1
{ "status": "online", "message": "All good." }
^
SyntaxError: Unexpected token in JSON at position 0
任何想法发生了什么?我认为它与从.txt文件中提取的事实有关?
UPDATE:
request('http://live.albiononline.com/status.txt', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // { "status": "online", "message": "All good." }
console.log(body.status); // undefined
}
});
它已经JSON对象。你不需要解析 – RaR
在我的问题中,我说'但是当我尝试登录时,body.status它是未定义的。' – TJH
你可以告诉我你的代码一次 –