我有:
<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);
}
但我在以下行收到错误“Uncaught SyntaxError: Unexpected token u”:
parsedNotifications = JSON.parse(notifications);
为什么会出现这个错误?