我: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/ –