Created
September 28, 2016 07:39
-
-
Save rheajt/c8d37b0b60eab3eb841822094270cac3 to your computer and use it in GitHub Desktop.
ajax requests without jquery
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
| // POST request | |
| var request = new XMLHttpRequest(); | |
| request.open('POST', '/my/url', true); | |
| request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); | |
| request.send(data); | |
| // GET request | |
| var request = new XMLHttpRequest(); | |
| request.open('GET', '/my/url', true); | |
| request.onload = function() { | |
| if (request.status >= 200 && request.status < 400) { | |
| // Success! | |
| var resp = request.responseText; | |
| } else { | |
| // We reached our target server, but it returned an error | |
| } | |
| }; | |
| request.onerror = function() { | |
| // There was a connection error of some sort | |
| }; | |
| request.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment