So, I've been writing a webpage and for part of this project I needed to access a JSON file in the JavaScript code. So I wrote the following:
function xhrSuccess () { this.callback.apply(this, this.arguments); }
function xhrError () { console.error(this.statusTest); }
function loadFile(sUR) {
var oReq = new XMLHttpRequest();
oReq.callback = readJSON;
oReq.onload = xhrSuccess;
oReq.onerror = xhrError;
oReq.open("GET", sURL, true);
oReq.send(null);
return oReq.callback();
}
function readJSON() {
console.log(this.responseText);
var my_JSON_object = JSON.parse(this.responseText);
return my_JSON_object;
}
And it's called in this way:
var data = loadFile("http://localhost:8000/json/data.json");
But I keep getting a few errors, and I'm not sure as to their meaning or even why I'm getting them.
Uncaught SyntaxError: Unexpected end of input
Uncaught SyntaxError: Unexpected token :
The first error occurs when I try to parse my JSON.
I have no idea what this means at all, because it normally relates to some sort of misplaced semi-colon or related object that causes a literal syntax error but the error was caught at the JSON.parse()
line so I'm not sure what I'm doing wrong.
Anyway, any help would be welcome.
And here's my JSON:
{ "data": [
{"file": "temp", "title": "This is a test", "date": "August 21, 2015"},
{"file": "temp2", "title": "This is also a test", "date": "August 20, 2015"},
{"file": "temp3", "title": "This is the final test", "date": "August 22, 2015"}
] }