[英]Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse
I am trying to load data from JSON to my website.我正在尝试将数据从 JSON 加载到我的网站。 Everything worked correctly for some time, but tonight I suddenly I started receiving the following error.
一切正常运行了一段时间,但今晚我突然开始收到以下错误。 (it works on localhost so far)
(到目前为止,它适用于本地主机)
Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at FileReader.<anonymous>
Javascript calling the JSON is following:调用 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');
I need to read data from the JSON, but now I cannot anymore.我需要从 JSON 中读取数据,但现在不能了。 Can someone help please?
有人可以帮忙吗?
EDIT: Everything works correctly on Safari.编辑:在 Safari 上一切正常。 The issue is happening in Chrome.
该问题发生在 Chrome 中。
As @abestrad pointed out, xhr.responseType = 'blob';
正如@abestrad 指出的,
xhr.responseType = 'blob';
is a possible issue and should be json
as outlined here .是一个可能的问题,应该是这里概述的
json
。
UPDATE: Try the following, which is working for me in chrome from same domain:更新:尝试以下操作,这对我在同一域的 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.