Revised HTTPRequest for AJAX stuff





17
Date Submitted Tue. Nov. 7th, 2006 9:04 PM
Revision 1 of 1
Scripter Fordiman
Tags ajax | JavaScript | XML
Comments 1 comments
This is a brief revision to my previous snippet. It is still what it was: a small, simple system for using XMLHttpRequest without using globals.

I'll put the API spec in the code this time, so as not to clutter the front page further.

Changes:

Changed name to HTTPRequest (far more accurate)
Added HEAD requests
Shortened code by making a GenericRequest function
Used a better method for getting an XMLHttpRequest object (checks multiple MS versions and implements the latest one)

HTTPRequest.buildQuery(queryData)
 

HTTPRequest.getXMLObject()
 

HTTPRequest.genericRequest(method, uri, content, callback, fallback)
 

HTTPRequest.GET / HTTPRequest.HEAD (uri, queryData, callback, fallback)
 

HTTPRequest.POST(uri, queryData, formData, callback, fallback)
 

var HTTPRequest=Object();
if (window.location.toString().substr(0,5).toLowerCase()=="file:")
        alert("XMLHttpRequest, cannot work\n"+
                                "from a local filesystem.\n"+
                                "Please upload your project to\n"+
                                "a webserver and try this again.");

HTTPRequest.buildQuery = function (query) {
        var data="";
        var first="?";
        for (i in query) {
                data+=first+escape(i)+"="+escape(query[i]);
                first="&";
        }
        return data;
}

HTTPRequest.getXMLObject = function () {
        if (typeof(XMLHttpRequest)!='undefined') return new XMLHttpRequest();
        var ax=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0',
                                        'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP',
                                        'Microsoft.XMLHTTP'];
        for (var i=0; i<ax.length; i++)
                try {
                        return new ActiveXObject(ax[i]);
                }
                catch (e) {}
        return null;
}

HTTPRequest.genericRequest = function (
        method,
        uri,
        content,
        callback,
        fallback) {
        var xmlhttp=HTTPRequest.getXMLObject();
        if (xmlhttp==null) {
                if (!HTTPRequest.complainedLoudly)
                        alert("Your browser does not support\n"+
                                                "XMLHttpRequest in any known form");
                HTTPRequest.complainedLoudly=true;
                return false;
        }
        xmlhttp.onreadystatechange=function () {
                if (xmlhttp.readyState==4)
                        if (xmlhttp.status==200)
                                callback(xmlhttp);
                        else
                                fallback(xmlhttp);
        }
        if (method=="POST") {
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlhttp.setRequestHeader("Content-length",postdata.length);
                xmlhttp.setRequestHeader("Connection","close");
        } else content="";
        xmlhttp.open(method.toUpperCase(),uri,true);
        xmlhttp.send(content);
        return true;
}

HTTPRequest.GET = function (url, query, callback, fallback) {
        return HTTPRequest.genericRequest(
                "GET",
                url+HTTPRequest.buildQuery(query),
                "",
                callback,
                fallback);
}

HTTPRequest.HEAD = function (url, query, callback, fallback) {
        return HTTPRequest.genericRequest(
                "HEAD",
                url+HTTPRequest.buildQuery(query),
                "",
                callback,
                fallback);
}

HTTPRequest.POST = function (url, query, form, callback, fallback) {
        return HTTPRequest.genericRequest(
                "POST",
                url+HTTPRequest.buildQuery(query),
                HTTPRequest.buildQuery(form).substr(1),
                callback,
                fallback);
}
 

Bryan Elliott

Comments

Comments Coll
Wed. Mar. 21st, 2007 12:09 PM    Newbie lando

Voting