Skip to content

Instantly share code, notes, and snippets.

@reidev275
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reidev275/7b03f42a479be595391f to your computer and use it in GitHub Desktop.
Save reidev275/7b03f42a479be595391f to your computer and use it in GitHub Desktop.
ajax without jQuery
var ajax = (function (ajax, json) {
ajax.send = function (obj) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status >= 200 && xmlhttp.status < 300 && obj.complete) {
obj.complete(json.parse(xmlhttp.responseText));
}
if (xmlhttp.status < 200 || xmlhttp.status >= 300) {
if (obj.error) obj.error(xmlhttp.response);
}
}
}
xmlhttp.open(obj.method, obj.url, true);
xmlhttp.setRequestHeader('Content-type', 'application/json');
xmlhttp.send(obj.data);
};
return ajax;
})(ajax || {}, JSON);
ajax.send({
url: 'http://myDomain.com/resource',
data: {
field: 'value'
},
method: 'POST',
complete: function(data) {
//do something with data
},
error: function(data) {
//handle error
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment