![](/img/trans.png)
[英]Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
[英]Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse
我正在尝试将数据从 JSON 加载到我的网站。 一切正常运行了一段时间,但今晚我突然开始收到以下错误。 (到目前为止,它适用于本地主机)
Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at FileReader.<anonymous>
调用 JSON 的 Javascript 如下:
function readJSON(path) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
var file = new File([this.response], 'temp');
var fileReader = new FileReader();
fileReader.addEventListener('load', function(){
// do stuff with fileReader.result
var volant = JSON.parse(fileReader.result);
// console.log(volant);
});
fileReader.readAsText(file);
}
}
xhr.send();
}
readJSON('https://volant.inexsda.cz/v1/workcamps.json');
我需要从 JSON 中读取数据,但现在不能了。 有人可以帮忙吗?
编辑:在 Safari 上一切正常。 该问题发生在 Chrome 中。
正如@abestrad 指出的, xhr.responseType = 'blob';
是一个可能的问题,应该是这里概述的json
。
更新:尝试以下操作,这对我在同一域的 chrome 中有效:
function readJSON(path) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.responseType = 'json';
xhr.onreadystatechange = function(e) {
if (xhr.readyState == 4) {
if (this.status == 200) {
console.log(this.response);
}
}
}
xhr.send();
}
readJSON('https://volant.inexsda.cz/v1/workcamps.json');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.