未捕获的SyntaxError:意外的令牌{script.js:5 request.onreadystatechange?

时间:2015-08-09 03:47:25

标签: javascript ajax json

运行下面的代码时,我收到错误:

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

2 个答案:

答案 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格式错误