Created
July 15, 2014 20:30
-
-
Save dan-diaz/2852d84e9f1f25b3ce71 to your computer and use it in GitHub Desktop.
Custom AJAX object that doesn't require 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
| DD.ajax = function(settings) { | |
| var ajax = new XMLHttpRequest(); | |
| // settings | |
| var method = settings.method || 'GET'; | |
| var url = settings.url || null; | |
| var async = settings.async || true; | |
| ajax.open(method, url, async); | |
| ajax.send(); | |
| ajax.onreadystatechange = function() { | |
| if(ajax.readyState === 1 && settings.beforeSend) { // opened | |
| settings.beforeSend(); | |
| } else if(ajax.readyState === 2 && settings.sent) { // headers_received | |
| settings.sent(); | |
| } else if(ajax.readyState === 3 && settings.loading) { // loading | |
| settings.loading(); | |
| } else if(ajax.readyState === 4 && settings.success) { // done | |
| settings.success(ajax.responseText); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment