Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active December 22, 2015 17:28
Show Gist options
  • Select an option

  • Save wilmoore/6505939 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/6505939 to your computer and use it in GitHub Desktop.
XMLHttpRequest vs. $.ajax
// does anyone like this API
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
// require the `xhr` module -- assumes we are using a sane module loader (ES6, Component, Browserify)
var xhr = require('./xhr');
// simple DOM API
var frm = document.getElementById('form');
// inject form and callback
xhr(frm, success);
// quick and dirty XHR abstraction -- definitely do not need jQuery for this sort of thing.
module.exports = function (form, success) {
var request = new XMLHttpRequest();
request.onload = success;
request.open(form.method, form.action);
request.send(new FormData(form));
return request;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment