簡體   English   中英

JSON輸入node.js意外結束jason.parse()

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

我正在嘗試解析通過以下(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',
            )
        );
    }

這是創建的JSON; {"Username":"nil","Code":"nil","DiscordID":"nil","IsVerified":"nil","RobloxID":"nil"}

我正在使用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");
        }
    });

但是,每當我嘗試運行該函數時,都會得到錯誤堆棧;

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)

關於我在這里缺少什么的任何想法? 我完全迷路了。

好吧,調用JSON.parse時發生錯誤。 這意味着返回的正文不是正確的JSON,根據請求文檔,它是有意義的,因為它是描述HTML頁面內容的字符串。 這意味着您正在HTML上運行JSON.parse,並且您的網址也必須不正確,這也是您發出請求的原因。 這可能是因為您正在向example.net發出請求? 我覺得基本網址應為其他網址。

在URL中查找故障的其他方法是檢查當您在瀏覽器中訪問php頁面並將其傳遞給參數時,該用戶對象是否在屏幕上可見。 然后確保該URL是正確的。 然后,最后應該檢查您的主體對象是否包含JSON。 如果它沒有打印出某種形式的JSON,則應打印正文並通讀HTML,因為這可能是PHP發送的錯誤,並且由於某種原因未被您的錯誤打印捕獲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM