Last active
December 22, 2015 17:28
-
-
Save wilmoore/6505939 to your computer and use it in GitHub Desktop.
XMLHttpRequest vs. $.ajax
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
| // does anyone like this API | |
| $.ajax({ | |
| type: "POST", | |
| url: url, | |
| data: data, | |
| success: success, | |
| dataType: dataType | |
| }); |
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
| // 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); |
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
| // 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