简体   繁体   English

SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous>

[英]SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

I am using an API for some information to display on my web page using https.get() in nodejs.我正在使用 API 获取一些信息,以便在 nodejs 中使用 https.get() 在我的 web 页面上显示。 But when I try to console log the response by parsing it as JSON this error is shown但是当我尝试通过将响应解析为 JSON 来控制台记录响应时,会显示此错误

SyntaxError: Unexpected end of JSON input at JSON.parse () at IncomingMessage.语法错误:在 IncomingMessage 的 JSON.parse () 处的 JSON 输入意外结束。 (C:\Users\Hardik Aggarwal\Desktop\RSC\app.js:17:33) at IncomingMessage.emit (events.js:315:20) at addChunk (_stream_readable.js:295:12) at readableAddChunk (_stream_readable.js:271:9) at IncomingMessage.Readable.push (_stream_readable.js:212:10) (C:\Users\Hardik Aggarwal\Desktop\RSC\app.js:17:33) 在 IncomingMessage.emit (events.js:315:20) 在 addChunk (_stream_readable.js:295:12) 在 readableAddChunk (_stream_readable. js:271:9) 在 IncomingMessage.Readable.push (_stream_readable.js:212:10)
at HTTPParser.parserOnBody (_http_common.js:132:24) at TLSSocket.socketOnData (_http_client.js:469:22) at TLSSocket.emit (events.js:315:20) at addChunk (_stream_readable.js:295:12)在 HTTPParser.parserOnBody (_http_common.js:132:24) 在 TLSSocket.socketOnData (_http_client.js:469:22) 在 TLSSocket.emit (events.js:315:20) 在 addChunk (_stream_readable.js:295:12)

The URL is sending the correct data in JSON format. URL 正在以 JSON 格式发送正确的数据。 The only problem is that JSON.parse() is not working on this data.唯一的问题是 JSON.parse() 无法处理此数据。 The code is代码是

app.get("/", function(req, res){
    https.get(url, "JSON", function(response){
        response.on("data", function(data){
            const currency=JSON.parse(data);
            console.log(currency);
        })
    })

    res.render("index");
})

When you use "data" event it means you treat response like a stream and it is not guaranteed that it will send all the data in one chunk.当您使用“数据”事件时,这意味着您将响应视为 stream 并且不能保证它会在一个块中发送所有数据。 So, in "data" event you should collect all the chunks and in "end" event you should try to parse the data.因此,在“数据”事件中,您应该收集所有块,在“结束”事件中,您应该尝试解析数据。 Also, don't forget to check response.statusCode for error.另外,不要忘记检查 response.statusCode 是否有错误。 Try this:尝试这个:

app.get("/", function(req, res){

https.get(url, "JSON", function(response){
    var data;
    response.on("data", function(chunk) {
      if (!data) {
        data = chunk;
      } else {
        data += chunk;
      }
    });

    response.on("end", function() {
        const currency=JSON.parse(data);
        console.log(currency);
        res.render("index");
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse(<anonymous>) 语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) on(&quot;数据&quot;) - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) on("data") 未捕获的语法错误:JSON 输入的意外结束:在 JSON.parse(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input : at JSON.parse (<anonymous>) SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) 在传入消息。<anonymous></anonymous></anonymous> - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at IncomingMessage.<anonymous> 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) 在 XMLHttpRequest。<anonymous> ((指数):32)</anonymous></anonymous> - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.<anonymous> ((index):32) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束 - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Unexpected end of JSON input at JSON.parse (<anonymous>) Uncaught SyntaxError:执行JSON.Parse时输入意外结束 - Uncaught SyntaxError: Unexpected end of input While doing JSON.Parse 语法错误:JSON.parse:数据意外结束 - SyntaxError: JSON.parse: unexpected end of data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM