Created
November 30, 2020 09:39
-
-
Save wpflames/9c16bca048dedd577d84916a31347356 to your computer and use it in GitHub Desktop.
AJAX request for a TXT file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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