Skip to content

Instantly share code, notes, and snippets.

@dan-diaz
Created July 15, 2014 20:30
Show Gist options
  • Select an option

  • Save dan-diaz/2852d84e9f1f25b3ce71 to your computer and use it in GitHub Desktop.

Select an option

Save dan-diaz/2852d84e9f1f25b3ce71 to your computer and use it in GitHub Desktop.
Custom AJAX object that doesn't require jQuery.
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