2017-06-18 154 views
0

我想解析发送回以下(PHP)的JSON;意外的JSON输入结束node.js jason.parse()

$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) 

任何想法,就是我在这里失踪?我完全迷失了。

回答

0

那么,错误发生时,JSON.parse被调用。这意味着返回的主体不是合适的JSON,根据请求文档,这是合理的,因为它是一个描述HTML页面内容的字符串。这意味着您正在HTML上运行JSON.parse,并且您的url也必须不正确,您也正在提出请求。这可能是因为你正在向example.net发送请求?我觉得基地网址应该是一个不同的网址。

找到你网址错误的其他方法是检查当你在浏览器中访问php页面并传递它时,用户对象在你的屏幕上可见。然后确保网址是正确的。最后,你应该检查你的body对象是否由JSON组成。如果它不打印某种形式的JSON,则应打印正文并通过HTML读取,因为它可能是PHP发送的错误,并且由于某种原因未被错误打印所捕获。