Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created November 30, 2020 10:26
Show Gist options
  • Select an option

  • Save wpflames/b9407ff9368ca665f0fc05fa0709c245 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/b9407ff9368ca665f0fc05fa0709c245 to your computer and use it in GitHub Desktop.
AJAX request for a TXT file - readystate
var btn = document.getElementById('button');
btn.addEventListener('click', loadText);
function loadText(){
//Create XHR Object
var xhr = new XMLHttpRequest();
//OPEN - type, url/file, async
xhr.open('GET', 'sample.txt', true);
xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
console.log(this.responseText)
}
}
//Sends request
xhr.send();
}
// readyState Values
// 0: request not initialized
// 1: server connection established
// 2: request recieved
// 3: processing request
// 4: request finished and response is ready
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment