Last active
January 19, 2018 15:56
-
-
Save duanckham/e5b690178b759603b81c to your computer and use it in GitHub Desktop.
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
var ajax = function(url, data) { | |
var wrap = function(method, cb) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open(method, url, true); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.send(data ? JSON.stringify(data) : null); | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState == 4 && xhr.status > 0) | |
cb(xhr); | |
} | |
return xhr; | |
}; | |
return { | |
get: function(cb) { | |
return wrap('GET', cb); | |
}, | |
post: function(cb) { | |
return wrap('POST', cb); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment