现在试图解决这个问题一段时间了。我知道堆栈溢出有类似的问题,但没有一个解决方案对我有用,我需要一些帮助。我收到错误消息
undefined:1
SyntaxError: Unexpected end of JSON input
at Object.parse (native)
at IncomingMessage.<anonymous> (B:\-\memeFinder.js:27:20)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
运行此代码时:
var http = require('http');
getRedditMemes();
function getRedditMemes () {
var options = {
host: 'www.reddit.com'
path: '/r/me_irl/hot/.json?count=20'
headers: {'User-agent':'xxxxx', 'Accept-Charset':'UTF-8'}
};
http.get(options, function (response) {
var str = '';
response.on('data', function (chunk) {
console.log(chunk)
str += chunk;
})
response.on('end', function () {
console.log(str);
var root = JSON.parse(str); //This line is where I'm getting the error
})
}).end();
}
我知道这里有其他答案可以解释错误是json不完整的结果,但是大多数适用的答案都说解析代码放在response.on('end')中我就是已经做了。这可能是一些愚蠢的小事,但非常感谢帮助,谢谢。
编辑: 感谢大家的回复。我很欣赏快速的帮助。
答案 0 :(得分:1)
请求返回301(重定向)错误,$scope.checfIfMobileNumIsValid = function()
{
var mobile=/^\d{10}$/;
if($scope.user.phone.match(mobile) || $scope.user.phone == '' || $scope.user.phone == 'null' || $scope.user.phone == null)
{
$scope.user.mobErrorMsg = 'Mobile number is not valid';
$scope.error = true;
}
else
{
$scope.user.mobErrorMsg = '';
$scope.error = false;
}
}
$scope.checkIfNameIsValid = function()
{
var name='/^[a-zA-Z ]+$/';
if($scope.user.name.match(name)||$scope.user.name == ''||$scope.user.name=='null'||$scope.user.name==null)
{
$scope.user.NameErrorMsg = 'Name is not valid';
$scope.error = true;
}
else
{
$scope.user.NameErrorMsg = '';
$scope.error = false;
}
}
$scope.checkIfAadharIsValid = function()
{
var aadhar='/^\d{12}$/';
if($scope.user.aadhar.match(aadhar) || $scope.user.aadhar == '' || $scope.user.aadhar == 'null' || $scope.user.aadhar == null)
{
$scope.user.aadharErrorMsg = 'Aadhar is not valid';
$scope.error = true;
}
else
{
$scope.user.aadharErrorMsg = '';
$scope.error = false;
}
}
包不在其后面。您可以构建逻辑以遵循重定向,也可以使用类似follow-redirects的包装器:
将您的第一行更改为:
http
答案 1 :(得分:0)
简单地说,变量str不是有效的JSON对象字符串。 尝试控制台记录变量块并查看。机会是块未定义或无效。