简体   繁体   中英

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse

I am trying to load data from JSON to my website. 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:

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. Can someone help please?

EDIT: Everything works correctly on Safari. The issue is happening in Chrome.

As @abestrad pointed out, xhr.responseType = 'blob'; is a possible issue and should be json as outlined here .

UPDATE: Try the following, which is working for me in chrome from same domain:

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM