Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created November 30, 2020 09:39
Show Gist options
  • Select an option

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

Select an option

Save wpflames/9c16bca048dedd577d84916a31347356 to your computer and use it in GitHub Desktop.
AJAX request for a TXT file
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.onload = function(){
if(this.status == 200){
console.log(this.responseText);
}
}
//Sends request
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment