简体   繁体   English

JSON输入node.js意外结束jason.parse()

[英]Unexpected end of JSON input node.js jason.parse()

I'm trying to parse JSON that is sent back with the following(PHP); 我正在尝试解析通过以下(PHP)发送回的JSON;

$UserData = $con->query("SELECT * FROM Discord WHERE RobloxID=".$UserId) or trigger_error($mysqli->error);
    $result = $UserData->fetch_array();
    if (!is_null($result[RobloxID])) {
        echo json_encode(array(
            'Username'  =>  $result[Username],
            'Code'      =>  $result[Code],
            'DiscordID' =>  $result[DiscordID],
            'IsVerified'=>  $result[IsVerified],
            'RobloxID'  =>  $result[RobloxID],
            )
        );
    } else {
        //$code = substr(md5(uniqid(mt_rand(), true)) , 0, 8);
        //$UserData->query("INSERT INTO Discord (Username,Code,DiscordID,IsVerified,RobloxID) VALUES ('$username','$code','$discordID','false','$UserId')");
        echo json_encode(array(
            'Username'  =>  'nil',
            'Code'      =>  'nil',
            'DiscordID' =>  'nil',
            'IsVerified'=>  'nil',
            'RobloxID'  =>  'nil',
            )
        );
    }

Here is the JSON created; 这是创建的JSON; {"Username":"nil","Code":"nil","DiscordID":"nil","IsVerified":"nil","RobloxID":"nil"}

I am using node.js to get what is returned; 我正在使用node.js来获取返回的内容;

request('http://example.net/API/verify.php?token=Nf_327&username=' + Username + '&UserId=' + UserID + '&discordid='+ msg.author.id, function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred 
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
        console.log('body:', body); // Print the HTML for the Google homepage.
        var info = JSON.parse(body);
        if (info.IsVerified == 'nil') {
            request('http://example.net/API/verify.php?token=Nf_328&username=' + Username + '&UserId=' + UserID + '&discordid='+ msg.author.id, function (error, response, body) {
                console.log('error:', error); // Print the error if one occurred 
                console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
                console.log('body:', body); // Print the HTML for the Google homepage.
            });
            msg.reply("Looks like its your first time trying to verify, I have DM'd you instructions on how to verify your account");
        }
    });

However whenever I try to run the function I get the error stack;` 但是,每当我尝试运行该函数时,都会得到错误堆栈;

Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Request._callback (/root/InfiusBot/commands/group/verify.js:41:22)
at Request.self.callback (/root/node_modules/request/request.js:188:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/root/node_modules/request/request.js:1171:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/root/node_modules/request/request.js:1091:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1047:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)

Any ideas as to what I am missing here? 关于我在这里缺少什么的任何想法? I am totally lost. 我完全迷路了。

Well, the error is occurring when JSON.parse is called. 好吧,调用JSON.parse时发生错误。 This means that the body that is returned is not proper JSON, which makes sense according to the request docs as it is a string that describes the content of the HTML page. 这意味着返回的正文不是正确的JSON,根据请求文档,它是有意义的,因为它是描述HTML页面内容的字符串。 This means that you are running JSON.parse on HTML and your url must be incorrect which you are making the request too. 这意味着您正在HTML上运行JSON.parse,并且您的网址也必须不正确,这也是您发出请求的原因。 This could be because you are making the request to example.net? 这可能是因为您正在向example.net发出请求? I feel that the base url should be a different URL. 我觉得基本网址应为其他网址。

Other methods to find the fault in your url is to check that when you visit the php page in your browser and pass it params the user object is visible on your screen. 在URL中查找故障的其他方法是检查当您在浏览器中访问php页面并将其传递给参数时,该用户对象是否在屏幕上可见。 Then make sure that the url is correct. 然后确保该URL是正确的。 Then lastly you should check that your body object consists of JSON. 然后,最后应该检查您的主体对象是否包含JSON。 If it does not print out some form of JSON, you should print the body and read through the HTML, as it could be an error that PHP is sending and for some reason was not caught by the error print that you have. 如果它没有打印出某种形式的JSON,则应打印正文并通读HTML,因为这可能是PHP发送的错误,并且由于某种原因未被您的错误打印捕获。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM