1

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.

5
  • Have you already checked the content you received from the HttpRequest? Please do so by adding a console.log(fileReader.result). Post that result and check it yourself for correctness. Commented Feb 15, 2019 at 22:26
  • 2
    Are you sure the response type is a 'blob'? Blob is an object containing the binary data. developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/… Commented Feb 15, 2019 at 22:29
  • console.log(fileReader.result) is empty Commented Feb 15, 2019 at 23:21
  • Not sure about the 'blob'. The whole "funny" issue is that code worked few hours ago and I did no changes in it. Plus it works in Safari and Firefox. Only Chrome is doing this :( Commented Feb 15, 2019 at 23:22
  • console.log(fileReader.result) is showing the content of JSON file in Safari (in Chrome it is empty) Commented Feb 15, 2019 at 23:23

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

12 Comments

Okay. I changed it, but now fileReader.result is null
@JakubŠnorbert, what does the network tab say when you fire off the request? There may be something in the headers of the response that could give more information.
Request URL: volant.inexsda.cz/v1/workcamps.json?page=1 Request Method: GET Status Code: 200 OK Remote Address: 83.167.225.248:443 Referrer Policy: no-referrer-when-downgrade Access-Control-Allow-Headers: authorization,x-goog-authuser Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE Access-Control-Allow-Origin: * Allow: POST, GET, OPTIONS, PUT, DELETE Provisional headers are shown Origin: localhost:8888 Referer: localhost:8888/aktivity/workcampy/kratkodobe-workcampy page:1
You're not making cross-domain calls, are you?
Running on a localhost with exception for cross-domain call
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.