我:JSON.parse会导致“未捕获的SyntaxError:意外的标记U”
<input type="hidden" id="notifications" value="@ViewBag.Notifications" />
当我把一个断点在这条线上,并检查该值,我看到的值是:
[{"id":"42647","isRead":0,"MessageType":3},{"id":"fsh3hg","isRead":0,"MessageType":2}]
我想在JavaScript页面加载时解析这个值,所以我写了:
var notifications = document.getElementById('notifications').value;
alert(notifications); // it prints undefined
alert(document.getElementById('notifications')); // it prints: Object HtmlSpanElement
var parsedNotifications;
if (notifications != '') {
parsedNotifications = JSON.parse(notifications);
}
,但我得到的错误“未捕获的SyntaxError:U在下面的行上有“nexpected token u”:
parsedNotifications = JSON.parse(notifications);
为什么会发生此错误?
'undefined'是无效的JSON。什么是生成的来源? – SLaks
这是一个类的列表.. –
该值未被正确输出。这里是一个值正确的小提琴和它正在工作:http://jsfiddle.net/A5Kf7/ –