I am trying to read test.txt through javascript and jquery in the HTML page textread.html stored in the same directory. But I get the following error:
Method 1 jQuery Method
jQuery error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
jQuery: `
$.getJSON('test.txt', function(data) {
console.log(data);
});
`
Method 2 javascript Method
javascript error: Cross origin requests are only supported for HTTP.
javascript:
`
if (xmlhttp != null) {
xmlhttp.open("GET","test.txt",false); // the false makes this synchronous!
xmlhttp.send();
var text = xmlhttp.responseText;
}
`
I have also tried alternative code
I am not hosting this application as I will be deploying it using PhoneGap as mobile applications.
Please provide me with a solution to read file under these circumstances. As an alternative solution is HTML5 FileReader advisable?