Hi I am trying to read in a .json file from my local machine. it will not read it in the code I am using is:
jQuery.getJSON('../json/test.json')
.done(function(data) {
var test = data;
alert("sucsess");
})
.fail(function(data){
alert("failed");
});
All I am getting from this is failed what am I doing wrong. I am not going through a server with this.
Check this fiddle
<title>reading Text files</title>
<body>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Print the contents of the file
var span = document.createElement('span');
span.innerHTML = ['<p>',e.target.result,'</p>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the file
//reader.readAsDataText(f,UTF-8);
//reader.readAsDataURL(f);
reader.readAsText(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
Use the File APIs. Here's a guide with sample code and demos:
Sorry but if you run the page from local drive ( address bar will have the form file:///path to your page) then you may read a file from disk, but if your run a script from webserver (http://......) you cannot read file from local drive. It's protect you from stealing information from user drive. You may need to upload a file in order to read it
EDIT: I have to take it back. New browser will allow you to read file based on user event. If user click file input and open a file then you can read it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With