Jazzo
12-23-2010, 10:50 PM
This is just a very-mini-library/convenience function that sends and receives AJAX information using get/post. It also supports using an object instead of a query string. It's long because it checks for all errors. The two functions are as follows:
ezjax.get(url, query, func);
ezjax.post(url, query, sendVars, func);
"url" is the URL without any query string. "query" is either a query string or an object which gets converted to a query string. "sendVars" is the same; it is the argument of request.send() and it can be an object. "func" is the function that is called after the request is complete. It takes one argument.
// Code by Julian Rosenblum
var ezjax = (function () {
var open,
createReq = function () {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}
else {
throw new Error('Browser does not support Ajax');
}
};
open = function (req, type, url, urlVars, sendVars, func) {
var urlString,
sendString,
toQueryString = function (obj) {
var i,
retVal = '?';
if (!obj) {
return '';
}
else if (typeof obj === 'string') {
if (obj.indexOf('?') === 0) {
return obj;
}
else {
return '?' + obj;
}
}
else if (typeof obj === 'object') {
for (i in obj) {
if (obj.hasOwnProperty(i)) {
retVal += escape(i) + '=' + escape(obj[i]) + '&';
}
}
return retVal.slice(0, -1);
}
else {
throw new TypeError();
}
};
urlString = url + toQueryString(urlVars);
sendString = (toQueryString(sendVars) === '?' ? null : toQueryString(sendVars).slice(1));
req.open(type, urlString, true);
if (type === 'POST') {
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
if (func) {
func(req.responseText);
}
}
}
};
req.send(sendString);
};
return {
get: function (url, urlVars, func) {
var req = createReq();
open(req, 'GET', url, urlVars, null, func);
},
post: function (url, urlVars, sendVars func) {
var req = createReq();
open(req, 'POST', url, urlVars, sendVars, func);
}
};
}());
ezjax.get(url, query, func);
ezjax.post(url, query, sendVars, func);
"url" is the URL without any query string. "query" is either a query string or an object which gets converted to a query string. "sendVars" is the same; it is the argument of request.send() and it can be an object. "func" is the function that is called after the request is complete. It takes one argument.
// Code by Julian Rosenblum
var ezjax = (function () {
var open,
createReq = function () {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}
else {
throw new Error('Browser does not support Ajax');
}
};
open = function (req, type, url, urlVars, sendVars, func) {
var urlString,
sendString,
toQueryString = function (obj) {
var i,
retVal = '?';
if (!obj) {
return '';
}
else if (typeof obj === 'string') {
if (obj.indexOf('?') === 0) {
return obj;
}
else {
return '?' + obj;
}
}
else if (typeof obj === 'object') {
for (i in obj) {
if (obj.hasOwnProperty(i)) {
retVal += escape(i) + '=' + escape(obj[i]) + '&';
}
}
return retVal.slice(0, -1);
}
else {
throw new TypeError();
}
};
urlString = url + toQueryString(urlVars);
sendString = (toQueryString(sendVars) === '?' ? null : toQueryString(sendVars).slice(1));
req.open(type, urlString, true);
if (type === 'POST') {
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
if (func) {
func(req.responseText);
}
}
}
};
req.send(sendString);
};
return {
get: function (url, urlVars, func) {
var req = createReq();
open(req, 'GET', url, urlVars, null, func);
},
post: function (url, urlVars, sendVars func) {
var req = createReq();
open(req, 'POST', url, urlVars, sendVars, func);
}
};
}());