Created
November 30, 2020 10:26
-
-
Save wpflames/b9407ff9368ca665f0fc05fa0709c245 to your computer and use it in GitHub Desktop.
AJAX request for a TXT file - readystate
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.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