I'm starting in my Javascript studies with interactions with the server, and right away I can not retrieve a text file to be shown via a Javascript alert. From what I've studied, the file must be on a server. In other words, to make a request one must be through an "http: //". At first, I'm doing this locally with Apache. And yet, it's not working.
Follow the code:
window.onload = function(){
document.getElementById("button").onclick = function(){
//alert("Ok");
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function(){
if( ajax.readyState == 4) {
alert(ajax.responseText);
}
}
ajax.open("POST", "http://localhost/ajax/texto.txt");
ajax.send(null); //Inicia a requisição para o servidor.
return false;
}
}
<h1 id="button">Teste</h1>
The result that appears to me:
Theerrorthatappearsintheconsole:
Please ... I look forward! Thanks!