[英]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.