JavaScript获取有效代码时出错
加载JSON文件的JavaScript代码出错:JavaScript获取有效代码时出错,javascript,ajax,json,error-handling,Javascript,Ajax,Json,Error Handling,加载JSON文件的JavaScript代码出错: function loadJSON(){ var xmlhttp; if (window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = funct
function loadJSON(){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
j = JSON.parse(xmlhttp.responseText);
//I get the "Uncaught SyntaxError: Unexpected token }" error on the line above
jsonLoaded = 1;
init();
};
};
xmlhttp.open("GET", "things.json", true);
xmlhttp.send();
};
为什么会出现此错误?几个小时前,同样的脚本还在运行
谢谢 尝试打印
xmlhttp.responseText
然后再尝试JSON.parse()。我怀疑它是否是您所期望的。@JonathonReinhart好的,我会试试。@JonathonReinhart它可能只是无效的JSON,因为它正在加载文件。