javascript - JSON.parse causes "Uncaught SyntaxError: Unexpected token u" -
i have:
<input type="hidden" id="notifications" value="@viewbag.notifications" />
when put breakpoint on line , check value, see value is:
[{"id":"42647","isread":0,"messagetype":3},{"id":"fsh3hg","isread":0,"messagetype":2}]
i want parse value in javascript when page loads, wrote:
var notifications = document.getelementbyid('notifications').value; alert(notifications); // prints undefined alert(document.getelementbyid('notifications')); // prints: object htmlspanelement var parsednotifications; if (notifications != '') { parsednotifications = json.parse(notifications); }
but error "uncaught syntaxerror: unexpected token u" on following line:
parsednotifications = json.parse(notifications);
why error occur?
you wrote:
alert(document.getelementbyid('notifications')); // prints: object htmlspanelement
in comment, htmlspanelement
clue something's wrong. apparently, page has <span>
id
same of hidden <input>
, document.getelementbyid
finds wrong element, , value
returns undefined
because <span>
doesn't have value.
change id
of <span>
other "notifications", , code should work.
Comments
Post a Comment