This code is used to read a .txt file in Javascript:
function readFile(fileName) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
console.log(his.responseText);
return this.responseText
}
};
xhttp.open("GET", fileName, true);
xhttp.send();}
This code print out the file contents into the console. I want to use the file content for further processing. When I try to read the contents using these two lines inside JavaScript:
contents = readFile("data.txt");
console.log(contents);
the console displays: undefined. How can I fix that?