简体   繁体   English

未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束

[英]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.

相关问题 未捕获的 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse(<anonymous>) 未捕获的语法错误:JSON 输入的意外结束:在 JSON.parse(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input : at JSON.parse (<anonymous>) Uncaught SyntaxError:执行JSON.Parse时输入意外结束 - Uncaught SyntaxError: Unexpected end of input While doing JSON.Parse 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) 在 XMLHttpRequest。<anonymous> ((指数):32)</anonymous></anonymous> - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.<anonymous> ((index):32) SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) on(&quot;数据&quot;) - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) on("data") 未捕获到的SyntaxError:意外令牌[与JSON.parse - Uncaught SyntaxError: Unexpected token [ with JSON.parse "未捕获的 SyntaxError:带有 JSON.parse 的意外标记" - Uncaught SyntaxError: Unexpected token with JSON.parse 语法错误:JSON.parse:数据意外结束 - SyntaxError: JSON.parse: unexpected end of data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM