XHConn - A Simple XMLHTTP Interface
LibraryXMLHTTP is a technology with which a developer can access external resources over HTTP from a static web page without ever having to reload the page itself. This library is meant to simplify and unify the code necessary to successfully send and receive simple data via XMLHTTP.
The latest version of the code will always be available from this page.
XHConn.js - (1.28KB) - 2005-04-08
XHConn is a data object with a single method. In order to use the object, you must first instantiate a copy of it.
var myConn = new XHConn();
After instantiating the object, it is good practice to check for success and act accordingly.
if (!myConn) // XMLHTTP not available. exit method/handle
error.
After you know you have a valid connection to work with, you need to define a function that will fire after your connection is complete. Your function should take a single argument which will be the XMLHttpRequest object with its members intact. [see: XMLHTTPRequest members]
var fnWhenDone = function (oXML) { alert(oXML.responseText);
};
Then the final step is to make your actual connection using the
connect member function of the XHConn object. The method takes
the following arguments:
sURL - String - The string that specifies
the path to the resource to connect to. (Note: Cross-domain scripting is
explicitly denied with XMLHTTP for security reasons. You may only access
files within the same domain as the host file.)sMethod - String - One of either "GET" or
"POST", which specify the type of HTTP request to make.sVars - String - A string of arguments,
encoded in the form var1=val1&var2=val2&.... These are the
variables that will be submitted along with the request.fnDone - Function - A reference to a
function that will be called upon completion of the HTTP request. A single
argument, the XMLHTTPRequest object, will be passed to the function.So a typical function call would look something like this:
myConn.connect("mypage.php", "POST", "foo=bar&baz=qux", fnWhenDone);
At this point your connection will be made and the results will be sent
to your function. The two major members of the XMLHTTPRequest object that
you will deal with are responseText and responseXML.
You can download this example script file to see what a complete implementation looks like:
testScript.js - (316 bytes) - 2004-08-30
XHConn has been tested on and is compatible with the following browsers:
setRequestHeader() method.)