Dead-simple XMLRequest framework for AJAX
15
This is a simple script to provide basic cross-platform XML request functionality in Javascript. It's meant to be the core component in any AJAX style framework. It is not an instantiable object, but instead is a namespaced microlibrary.
Calling is easy:
XMLRequest.GET(uri, query, callback, fallback)
uri: The location you're after
query: an associative array of form data to provide via the URL
callback: callback function of the form myCallbackFunction(objXMLHttpRequest), which is called upon successful (response = 200 OK) retrieval of the XML data
fallback: myFallbackFunction(objXMLHttpRequest), which is called upon failed (response != 200 OK) retrieval of the XML data.
XMLRequest.POST(uri, query, form, callback, fallback)
Similar, but does the query using the POST method. 'query' is the URL-appended data, still in associative array form, and 'form' is the same for the POST data.
Calling is easy:
XMLRequest.GET(uri, query, callback, fallback)
uri: The location you're after
query: an associative array of form data to provide via the URL
callback: callback function of the form myCallbackFunction(objXMLHttpRequest), which is called upon successful (response = 200 OK) retrieval of the XML data
fallback: myFallbackFunction(objXMLHttpRequest), which is called upon failed (response != 200 OK) retrieval of the XML data.
XMLRequest.POST(uri, query, form, callback, fallback)
Similar, but does the query using the POST method. 'query' is the URL-appended data, still in associative array form, and 'form' is the same for the POST data.
var XMLRequest=Object();
XMLRequest.buildQuery = function (query) {
var data="";
var first="?";
for (i in query) {
data+=first+escape(i)+"="+escape(query[i]);
first="&";
}
return data;
}
XMLRequest.GET = function (url, query, callback, fallback) {
var xmlhttp=null;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest()
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null) {
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) callback(xmlhttp);
else fallback(xmlhttp);
}
}
var data = XMLRequest.buildQuery(query);
xmlhttp.open("GET",url+data,true);
xmlhttp.send("");
}else{
alert("Your browser does not support XMLHTTP.")
}
}
XMLRequest.POST = function (url, query, form, callback, fallback) {
var xmlhttp=null;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest()
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null) {
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
callback(xmlhttp);
} else {
fallback(xmlhttp);
}
}
}
var urldata = XMLRequest.buildQuery(query);
var postdata = XMLRequest.buildQuery(form).substr(1);
xmlhttp.open("POST",url+urldata,true);
xmlhttp.setRequestHeader("Content-type" , "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", postdata.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(postdata);
}else{
alert("Your browser does not support XMLHTTP.")
}
}
XMLRequest.buildQuery = function (query) {
var data="";
var first="?";
for (i in query) {
data+=first+escape(i)+"="+escape(query[i]);
first="&";
}
return data;
}
XMLRequest.GET = function (url, query, callback, fallback) {
var xmlhttp=null;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest()
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null) {
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) callback(xmlhttp);
else fallback(xmlhttp);
}
}
var data = XMLRequest.buildQuery(query);
xmlhttp.open("GET",url+data,true);
xmlhttp.send("");
}else{
alert("Your browser does not support XMLHTTP.")
}
}
XMLRequest.POST = function (url, query, form, callback, fallback) {
var xmlhttp=null;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest()
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null) {
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
callback(xmlhttp);
} else {
fallback(xmlhttp);
}
}
}
var urldata = XMLRequest.buildQuery(query);
var postdata = XMLRequest.buildQuery(form).substr(1);
xmlhttp.open("POST",url+urldata,true);
xmlhttp.setRequestHeader("Content-type" , "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", postdata.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(postdata);
}else{
alert("Your browser does not support XMLHTTP.")
}
}






There are currently no comments for this snippet.