运行下面的代码时,我收到错误:
var request=new XMLHttpRequest();
request.open('GET','data.json');
request.onreadystatechange=function(){
if((request.readyState===4)&&(request.status==200)){
var customers=JSON.parse(request.responseText);
console.log(customers);
}
}
request.send();
错误是:
Uncaught SyntaxError: Unexpected token { script.js:5 request.onreadystatechange
答案 0 :(得分:0)
问题出在JSON.parse中。在上一行中添加console.log
console.log(request.responseText);
var customers=JSON.parse(request.responseText);
console.log(customers);
还控制responseText没有utf-8 BOM。
答案 1 :(得分:0)
可能是您的data.json格式错误