Skip to content

Instantly share code, notes, and snippets.

@tmessi
Last active August 29, 2015 14:08
Show Gist options
  • Save tmessi/f4340c916d0a4ee10fba to your computer and use it in GitHub Desktop.
Save tmessi/f4340c916d0a4ee10fba to your computer and use it in GitHub Desktop.
Ajax without jquery
/**
* Taken from http://stackoverflow.com/a/18078705 with minor tweaks.
*/
var ajax = {};
/**
* Attempt to get the XMLHttpRequest object. If unavailable try fallbacks of increasing age.
*/
ajax.x = function() {
if (typeof XMLHttpRequest !== 'undefined') {
return new XMLHttpRequest();
}
var versions = [
"MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"
];
var xhr;
for(var i = 0; i < versions.length; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
} catch (e) {}
}
return xhr;
};
/**
* Send the request.
*
* @param url - Send the request to url.
* @param headers - A list of header tuples. The tulpe is [<header-type>, <header-value>].
* @param callback - Callback function to handle the response.
* @param method - The type of request, generally either 'POST' or 'GET'.
* @param data - Data to send as part of the request.
* @param async - True if the request should be asynchronous, False if synchronous. Default is true.
*/
ajax.send = function(url, headers, callback, method, data, async) {
if (typeof async === 'undefined') async = true; // default to async
var x = ajax.x();
x.open(method, url, async);
x.onreadystatechange = function() {
if (x.readyState == 4) {
callback(x.responseText)
}
};
if (method == 'POST') {
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
for (var i in headers) {
x.setRequestHeader(header[i][0], header[i][1]);
}
x.send(data)
};
/**
* Send a GET request
*
* @param url - Send the get request to url.
* @param data - Dictionary of key, value parameters to send.
* @param headers - A list of header tuples. The tulpe is [<header-type>, <header-value>].
* @param callback - Callback function to handle the response.
* @param async - True if the request should be asynchronous, False if synchronous. Default is true.
*/
ajax.get = function(url, data, headers, callback, async) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
ajax.send(url + '?' + query.join('&'), headers, callback, 'GET', null, async)
};
/**
* Send a POST request
*
* @param url - Send the get request to url.
* @param data - Dictionary of key, value parameters to send.
* @param headers - A list of header tuples. The tulpe is [<header-type>, <header-value>].
* @param callback - Callback function to handle the response.
* @param async - True if the request should be asynchronous, False if synchronous. Default is true.
*/
ajax.post = function(url, data, headers, callback, async) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
ajax.send(url, headers, callback, 'POST', query.join('&'), async)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment