As I embark on my journey to understand how to read a text file locally using JavaScript and Ajax, I find myself lost in the sea of online tutorials. Despite diligently following these resources, I am unable to successfully display the contents of the .txt file.
function loadDoc(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
xmlhttp.open("GET","names.txt",true);
allText = xmlhttp.responseText;
lines = xmlhttp.responseText.split("\n");
}
}
xmlhttp.send(null);
document.getElementById("myDiv").innerHTML=allText;
}
My understanding is that this function should update the designated div element (id "myDiv") with the content of the text file. However, despite experimenting with different approaches, I have not been able to achieve the desired outcome. Any guidance from those more familiar with JavaScript and Ajax would be greatly appreciated as I continue to navigate this learning process.