I have:
<input type="hidden" id="notifications" value="@ViewBag.Notifications" />
When I put a breakpoint on this line and check the value, I see that the value is:
[{"id":"42647","isRead":0,"MessageType":3},{"id":"fsh3hg","isRead":0,"MessageType":2}]
I want to parse this value in JavaScript when the page loads, so I wrote:
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);
}
but I get the error "Uncaught SyntaxError: Unexpected token u" on the following line:
parsedNotifications = JSON.parse(notifications);
Why does this error occur?
undefinedis not valid JSON. What's the generated source?