Last active
April 26, 2017 17:36
-
-
Save earthchie/89d3e91e2d1050e1dc5b89cb27630bc9 to your computer and use it in GitHub Desktop.
This file contains 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 = { | |
x: [], | |
abortAll: function() { | |
var i; | |
for (i in ajax.x) { | |
if (Object.prototype.hasOwnProperty.call(ajax.x, i)) { | |
ajax.x[i].abort(); | |
} | |
} | |
}, | |
xhr: function() { | |
var a; | |
for (a = 0; a < 4; a = a + 1) { | |
try { | |
return a ? new ActiveXObject(['', 'Msxml2', 'Msxml3', 'Microsoft'][a] + '.XMLHTTP') : new XMLHttpRequest(); | |
} catch (e) {} | |
} | |
}, | |
sendRequest: function(verb, url, data, success, error) { | |
data = data || null; | |
var xhttp = new ajax.xhr(), | |
serialize = '', | |
i; | |
if (typeof data === 'object') { | |
for (i in data) { | |
if (Object.prototype.hasOwnProperty.call(data, i)) { | |
serialize += '&' + i + '=' + data[i]; | |
} | |
} | |
data = serialize.substring(1); | |
} | |
xhttp.onreadystatechange = function() { | |
if (xhttp.readyState === 4) { | |
var i; | |
for (i in ajax.x) { | |
if (ajax.x[i] === xhttp) { | |
ajax.x.splice(i, 1); | |
break; | |
} | |
} | |
if (xhttp.status === 200) { | |
if (typeof success === 'function') { | |
success(xhttp.responseText); | |
} | |
} else { | |
if (typeof error === 'function') { | |
error(xhttp); | |
} | |
} | |
} | |
}; | |
xhttp.open(verb.toString().toUpperCase(), url, true); | |
xhttp.send(data); | |
ajax.x.push(xhttp); | |
}, | |
GET: function(url, success, error) { | |
ajax.sendRequest('GET', url, null, success, error); | |
}, | |
POST: function(options, success, error) { | |
ajax.sendRequest('POST', options.url || options, options.data || null, success, error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment