Node.js JSON解析=>;SyntaxError:意外标记<;在JSON.parse(<;匿名>;)的位置0处的JSON中

Node.js JSON解析=>;SyntaxError:意外标记<;在JSON.parse(<;匿名>;)的位置0处的JSON中,node.js,json,api,express,Node.js,Json,Api,Express,我试图从服务器获取信息,但若要解析答案,则出现错误 我的代码: const data = ["10093"] const options = { auth: userpsw, method: 'GET', CURLOPT_POSTFIELDS: data }; const request = http.request(url, options, function(response) { response.on("data",

我试图从服务器获取信息,但若要解析答案,则出现错误

我的代码:

const data = ["10093"]

const options = {
    auth: userpsw,
    method: 'GET',
    CURLOPT_POSTFIELDS: data
};

const request = http.request(url, options, function(response) {
  response.on("data", function(data){
    console.log(JSON.parse(data));
  })
})
答复:

undefined:1
<br />
^

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
然后我做了以下按摩:

Buffer 3c 62 72 20 2f 3e 0a 3c 62 3e 57 61 72 6e 69 6e 67 3c 2f 62 3e 3a 20 20 69 6d 70 6c 6f 64 65 28 29 3a 20 49 6e 76 61 6c 69 64 20 61 72 67 75 6d 65 6e ... 282 more bytes

什么我看不见?如何阅读答案?

我建议使用请求模块而不是HTTP:

var options = {
    hostname: '_your_url',
    port: 'your_port',
    path: '/your_path',
    method: 'GET',
    json:true
}
request(options, function(error, response, body){
    if(error) console.log(error);
    else console.log(body);
});

您的响应不是JSON格式,这就是您在尝试解析响应时出错的原因。请使用try和catch来处理错误。

您的资源没有返回JSON。它将返回以

\n开头的文本警告:内爆():无效参数(您显示的缓冲区文本就是这样说的)。
var options = {
    hostname: '_your_url',
    port: 'your_port',
    path: '/your_path',
    method: 'GET',
    json:true
}
request(options, function(error, response, body){
    if(error) console.log(error);
    else console.log(body);
});