Final solution: There was something wrong with the formatting of my .json
file deeper down than the snippet I included in the question. Everything is working now. Big thanks to everyone in the comments and I'm marking @TWLATL's answer as correct because it's a nicely formatted function solution I can use in projects later.
I'm trying to access my data.json
file from script.js
(both in the root
directory) and it doesn't seem to be running at all. Before I put in error handling, I was getting no indication the function ran. Thanks to comments, I replaced .error
with .fail
, but I still can't figure out why it's failing.
index.html
<body>
<h1>JSON Render</h1>
<div id="fileViewer"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="script.js"></script>
</body>
script.js
$.getJSON('data.json', function(data) {
console.log('json grabbed')
console.log(data);
})
.fail(function(data) {
console.log("GetJSON Error!");
console.log(data);
});
$.ajax({
url: 'data.json',
dataType: 'json',
success: function( data ) {
console.log( "SUCCESS: " );
console.log( data );
},
error: function( data ) {
console.log( "AJAX ERROR: " );
console.log( data );
}
});
data.json (sample)
{
"children": [
{
"name": "Main Folder",
"type": "folder"
},
{
"name": "Private folder",
"type": "folder"
}
]
}
I've been looking around on Stack Overflow and no solutions I've found seem to solve my problem. Help!
Edit: Added jQuery CDN to my index.html
. It was there. I just forgot to put it in the question.
Edit 2: Updated script.js to reflect the code that is returning the screenshots below. (screenshot cut off in the middle of the object. I can include more if the context of the object is actually relevant.)
Edit 3: New AJAX Error handling: The new ajax function:
$.ajax({
url: 'data.json',
dataType: 'json',
success: function( data ) {
console.log( "SUCCESS: " );
console.log( data );
},
error: function(data, textStatus, errorThrown) {
console.log("textStatus: ",textStatus);
console.log("errorThrown: ",errorThrown);
}
});
And I am now wonderfully getting this console output: