seitime-frappe/js/wn/xmlhttp.js
2011-09-15 12:55:43 +05:30

44 lines
1 KiB
JavaScript

wn.xmlhttp = {
request: function() {
if ( window.XMLHttpRequest ) // Gecko
return new XMLHttpRequest() ;
else if ( window.ActiveXObject ) // IE
return new ActiveXObject("MsXml2.XmlHttp") ;
},
complete: function(req, callback, url) {
if (req.status==200 || req.status==304) {
callback(req.responseText);
} else {
alert(url +' request error: ' + req.statusText + ' (' + req.status + ')' ) ;
}
},
get: function(url, callback, args, async) {
// async by default
if(async === null) async=true;
var req = wn.xmlhttp.request();
// for async type
req.onreadystatechange = function() {
if (req.readyState==4) {
wn.xmlhttp.complete(req, callback, url)
}
}
// separator can be & or ?
// based on if there are already arguments
var sep = ((args && args.indexOf('?'))==-1) ? '?' : '&';
// add arguments to url
var u = args ? (url + sep + args) : url;
// call the server
req.open('GET', u, async);
req.send(null);
// for sync
if(!async) {
wn.xmlhttp.complete(req, callback, url)
}
}
}